根据另一列的条件为特定列着色
Color Specific columns based on condition of another column
我正在尝试根据 Col1
和 return 其他没有任何颜色的列为 "Col2", "Col3", "Col4", "Col5", "Col6"
列着色。
示例数据
from random import randint
x = [randint(0, 1) for p in range(0, 10)]
sample_dict = {"Col1": [randint(0, 1) for p in range(0, 10)],
"Col2": [randint(0, 1) for p in range(0, 10)],
"Col3": [randint(0, 1) for p in range(0, 10)],
"Col4": [randint(0, 1) for p in range(0, 10)],
"Col5": [randint(0, 1) for p in range(0, 10)],
"Col6": [randint(0, 1) for p in range(0, 10)]}
abcd = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
sample = pd.DataFrame(sample_dict)
data = pd.concat([sample, abcd], axis=1)
Col1 Col2 Col3 Col4 Col5 Col6 A B C D
0 0 1 1 1 0 1 -1.358769 -0.310314 -1.056405 -0.567577
1 0 1 0 1 1 0 -0.539893 -0.139629 0.270759 -0.430564
2 1 1 1 0 1 0 -0.009886 0.023482 0.966884 0.612875
3 0 1 1 1 0 1 0.962987 0.191210 -0.228937 -0.338486
4 1 1 1 0 0 1 -0.867326 -0.461046 0.832390 0.956392
5 0 0 0 0 1 1 0.348276 0.711152 -1.016820 0.394526
6 1 1 1 1 0 0 0.622317 0.519261 -2.022494 -1.170836
7 0 1 0 0 1 0 0.033249 0.491181 -0.065532 0.936868
8 1 0 0 1 1 1 1.064310 -0.257726 -0.197229 0.348314
9 0 1 0 1 1 0 0.017713 -0.624656 -0.341611 -1.433317
到目前为止,我可以通过
给它上色
data["Col1", "Col2", "Col3", "Col4", "Col5", "Col6"].style.apply(lambda x: ["background-color: orange" if v != x.iloc[0] else "background_color: none" for v in x], axis=1)
但无法 return 其他列。
如何根据Col1
给第"Col2", "Col3", "Col4", "Col5", "Col6"
列添加颜色?是否可以为 "Col2", "Col3", "Col4", "Col5", "Col6"
中的每一列应用不同的颜色?
您可以按列迭代并使用颜色字典:
colors = {'Col2': 'orange',
'Col3': 'lightblue',
'Col4': 'lightgreen',
'Col5': 'lightpink',
'Col6': 'yellow'
}
import numpy as np
(data
.style.apply(lambda c: np.where(sample['Col1'].eq(c),
'background-color: none',
f'background-color: {colors.get(c.name, "none")}')
)
)
输出:
我正在尝试根据 Col1
和 return 其他没有任何颜色的列为 "Col2", "Col3", "Col4", "Col5", "Col6"
列着色。
示例数据
from random import randint
x = [randint(0, 1) for p in range(0, 10)]
sample_dict = {"Col1": [randint(0, 1) for p in range(0, 10)],
"Col2": [randint(0, 1) for p in range(0, 10)],
"Col3": [randint(0, 1) for p in range(0, 10)],
"Col4": [randint(0, 1) for p in range(0, 10)],
"Col5": [randint(0, 1) for p in range(0, 10)],
"Col6": [randint(0, 1) for p in range(0, 10)]}
abcd = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
sample = pd.DataFrame(sample_dict)
data = pd.concat([sample, abcd], axis=1)
Col1 Col2 Col3 Col4 Col5 Col6 A B C D
0 0 1 1 1 0 1 -1.358769 -0.310314 -1.056405 -0.567577
1 0 1 0 1 1 0 -0.539893 -0.139629 0.270759 -0.430564
2 1 1 1 0 1 0 -0.009886 0.023482 0.966884 0.612875
3 0 1 1 1 0 1 0.962987 0.191210 -0.228937 -0.338486
4 1 1 1 0 0 1 -0.867326 -0.461046 0.832390 0.956392
5 0 0 0 0 1 1 0.348276 0.711152 -1.016820 0.394526
6 1 1 1 1 0 0 0.622317 0.519261 -2.022494 -1.170836
7 0 1 0 0 1 0 0.033249 0.491181 -0.065532 0.936868
8 1 0 0 1 1 1 1.064310 -0.257726 -0.197229 0.348314
9 0 1 0 1 1 0 0.017713 -0.624656 -0.341611 -1.433317
到目前为止,我可以通过
给它上色data["Col1", "Col2", "Col3", "Col4", "Col5", "Col6"].style.apply(lambda x: ["background-color: orange" if v != x.iloc[0] else "background_color: none" for v in x], axis=1)
但无法 return 其他列。
如何根据Col1
给第"Col2", "Col3", "Col4", "Col5", "Col6"
列添加颜色?是否可以为 "Col2", "Col3", "Col4", "Col5", "Col6"
中的每一列应用不同的颜色?
您可以按列迭代并使用颜色字典:
colors = {'Col2': 'orange',
'Col3': 'lightblue',
'Col4': 'lightgreen',
'Col5': 'lightpink',
'Col6': 'yellow'
}
import numpy as np
(data
.style.apply(lambda c: np.where(sample['Col1'].eq(c),
'background-color: none',
f'background-color: {colors.get(c.name, "none")}')
)
)
输出: