import pandas as pd
test_df =pd.DataFrame({"col1":[1,12,3,4],
"col2":[3,14,5,6],
"col3":[4,5,6,7]})
print(test_df)
col1 col2 col3
0 1 3 4
1 12 14 5
2 3 5 6
3 4 6 7
def highlight(row):
ret =["" for _ in row.index]
if row['col3'] == 5:
ret[row.index.get_loc('col3')] ="background-color: #f2f20a"
if row['col2'] == 5:
ret[row.index.get_loc('col2')] ="background-color: #f2f20a"
return ret
dd= test_df.style.apply(highlight, axis=1)
print(dd)
col1 col2 col3
0 1 3 4
1 12 14 **5**
2 3 **5** 6
3 4 6 7
如何单独创建只有突出显示行的数据框或 excel?在这种情况下,只有第 1、2 行将出现在单独的 excel 或数据框中。
提前致谢!