Search code examples
pythonpandasdataframecolorspython-applymap

How to color newly added values in a data frame with Pandas?


I'd like to highlight (or color) new values in a DataFrame that previously were NaNs.

I have 2 data frames with the same index.

One with NaNs

df_nan = pd.DataFrame(np.random.randint(10, size = (10, 10))).replace(8, np.nan)
df_nan

    0       1       2   3   4   5       6   7   8       9
0   NaN     NaN     7   0   0   0.0     0   6   2.0     4.0
1   6.0     3.0     7   1   0   5.0     3   5   NaN     7.0
2   5.0     6.0     0   1   0   NaN     2   4   4.0     7.0
3   NaN     2.0     6   3   1   4.0     9   0   5.0     3.0
4   9.0     0.0     5   2   2   5.0     6   0   9.0     1.0
5   9.0     4.0     0   2   3   9.0     2   9   3.0     4.0
6   4.0     4.0     9   6   7   1.0     7   9   5.0     NaN
7   0.0     NaN     9   2   0   5.0     7   6   3.0     NaN
8   9.0     9.0     0   0   4   6.0     3   3   1.0     7.0
9   3.0     6.0     3   2   7   1.0     6   5   2.0     9.0

Another one (an "updated" one) where NaNs have been replaced by new values (means of each column)

df_new = df_nan.replace(np.nan, np.mean(df_nan))
df_new

    0       1       2  3    4   5       6   7   8       9
0   5.62    4.25    7   0   0   0.0     0   6   2.00    4.00
1   6.00    3.00    7   1   0   5.0     3   5   3.77    7.00
2   5.00    6.00    0   1   0   4.0     2   4   4.00    7.00
3   5.62    2.00    6   3   1   4.0     9   0   5.00    3.00
4   9.00    0.00    5   2   2   5.0     6   0   9.00    1.00
5   9.00    4.00    0   2   3   9.0     2   9   3.00    4.00
6   4.00    4.00    9   6   7   1.0     7   9   5.00    5.25
7   0.00    4.25    9   2   0   5.0     7   6   3.00    5.25
8   9.00    9.00    0   0   4   6.0     3   3   1.00    7.00
9   3.00    6.00    3   2   7   1.0     6   5   2.00    9.00

How can i highlight or color the new values (means) using Pandas .style and .applymap() methods ?

Any help would be much appreciated !


Solution

  • style = 'color: yellow; background: red; border: 3px solid green'
    funct = lambda d: df_nan.isnull() * style
    df_new.style.apply(funct, None)
    

    enter image description here