如何根据熊猫中的给定键更改背景单元格颜色?

时间:2018-03-31 17:11:59

标签: python pandas numpy dataframe

我有以下数据框:

df = pd.DataFrame({"A":["foo", "foo", "foo", "bar"],  
                   "B":["A","A","B","A"], 
                   "C":[0,3,1,1]})

如何更改列A和B的单元格颜色,按其值分组。我的意思是,这将是理想的输出:

enter image description here

可能是这样的:

df.groupby(by=['A', 'B']).style.change_background()

由于真正的数据帧有数百行,我会自动分配颜色。

1 个答案:

答案 0 :(得分:2)

from matplotlib.pyplot import cm 
from matplotlib.colors import to_hex

# convert groups to indices    
g = pd.Categorical(df.A + df.B).codes

# generate a list hex colors
color = cm.rainbow(g / g.max())
hex_color = [to_hex(col) for col in color]
print('generate colors: ', hex_color)

# apply the colors to style for columns A and B
df.style.apply(
    lambda col: ['background-color: %s' % (hex_color[i]) for i in range(col.size)], 
    subset=['A', 'B']
)

enter image description here

由Antonvbr更新:(通过此解决方案,我使用seaborn添加了解决方案。)

import seaborn as sns
import pandas as pd

df = pd.DataFrame({"A":["foo", "foo", "foo", "bar"],  
                   "B":["A","A","B","A"], 
                   "C":[0,3,1,1]})

g = pd.Categorical(df.A + df.B).codes # convert groups to indices  
n = np.unique(g).size
palette = sns.color_palette("hls", n).as_hex()

# apply the colors to style for columns A and B
df.style.apply(
    lambda x: ['background-color: {}'.format(palette[i]) for i in g], 
    subset=['A', 'B']
)

enter image description here