我有一个包含6列的dataFrame。我想对它们的两列进行条件格式化。所以我的dataFrame看起来像这样
而且我想突出显示College和College_F2列中的重复值。之后,我的数据框将如下所示
为此编写的代码如下所示:dataFrame_file = pd.read_excel(util.comcastFile2Path(), sheet_name='Sheet1')
def dummy_color(x):
c1 = 'background-color:red'
c2 = ''
cond = dataFrame_file.stack().duplicated(keep=False).unstack()
df1 = pd.DataFrame(np.where(cond, c1, c2), columns=x.columns, index=x.index)
return df1
dataFrame_file.style.apply(dummy_color,axis=None,subset=['College', 'College_F2']).to_excel(util.comcastFile2Path)
此代码给我的错误是
ValueError: Shape of passed values is (6, 6), indices imply (6, 2)
我使用的是PyCharm。如何解决此问题?
提前感谢。
def dummy_color(x):
color = 'red' if (len(dataFrame_file[dataFrame_file['College'] == x]) + len(dataFrame_file[dataFrame_file['College_F2'] == x])) > 1 else ''
return 'background-color: %s' % color
dataFrame_file.style.applymap(dummy_color, subset=['College', 'College_F2']).to_excel(util.comcastFile2Path)