【发布时间】:2022-02-21 18:04:04
【问题描述】:
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 或数据框中。
提前致谢!
【问题讨论】:
-
您要选择第 1,2 行来创建一个新的 DataFrame 吗?这里有什么问题?
标签: python pandas dataframe exploratory-data-analysis styler