![](/img/trans.png)
[英]Get sum of two columns based on conditions of other columns in a Pandas Dataframe
[英]Highlight pandas dataframe columns based on two different conditions
我有以下 dataframe comp
:
time first_max name second_max name.1 Perceived OoM.1 Perceived OoM.2
0 0.000000 18 shoulder_center 9 right_hip shoulder_center shoulder_center
1 0.010000 18 shoulder_center 9 right_hip shoulder_center shoulder_center
2 0.020000 18 right_hip 9 right_hip shoulder_center right_hip
3 0.030000 18 shoulder_center 9 right_hip shoulder_center right_hip
我有这个 function 根据name == Perceived OoM.1
突出显示整行:
def highlight_col(x):
df = x.copy()
mask = df['name'] == df['Perceived OoM.1']
df.loc[mask, :] = 'background-color: yellow'
df.loc[~mask,:] = 'background-color: ""'
return df
comp.style.apply(highlight_col, axis=None)
但是,如果name == Perceived OoM.2
,我想找出一种方法为整行着色另一种颜色。 基本上,如果 name == Perceived OoM.1 我希望该行为黄色,否则如果name == Perceived OoM.2
name == Perceived OoM.1
该行为蓝色。
但我似乎无法在我的 function 中使用这种情况。
有什么帮助吗?
创建另一个掩码并以相同的方式传递,也使用默认空值DataFrame
构造函数:
def highlight_col(x):
df = pd.DataFrame('', index=x.index, columns=x.columns)
mask1 = x['name'] == x['Perceived OoM.1']
mask2 = x['name'] == x['Perceived OoM.2']
df.loc[mask1, :] = 'background-color: yellow'
df.loc[mask2, :] = 'background-color: blue'
return df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.