数据如下所示
Code Col1 Col2 col3 col4 col5
0 123 5 66 1.7 7
1 123 1 4 12 -8
2 123 9 5 -7 0
3 123 34.4 0 4.789 2
我应该能够忽略在某些列上应用样式(例如在本例中为 col1)不必总是 col1 我应该能够通过指定列的名称来忽略它。并非每一列都应该具有相同的样式,例如 col 2 和 5 我想用绿色的正值和红色的负值着色但是对于 col3 我想用低于 50 的紫色值和黄色的其余值着色
实际的数据集有几十列,对于作为列名函数的着色代码,每个列都有不同的条件。
我试过的:
import pandas as pd
import numpy as np
df = pd.read_excel('Data.xls', sheet_name='test')
styler = df.style
def _color_red_or_green(val):
color = 'red' if val < 0 else 'green'
return 'color: %s' % color
styler.applymap(_color_red_or_green)
styler.to_excel('Output.xlsx')
但这并没有给我任何指定列名的方法,尽管它确实将我的所有数据着色为红色或绿色,但我试图将列名作为 _color_red_or_green
的参数传递for col in df.dtypes.items():
styler.applymap(_color_red_or_green(col[0]))
并相应地调整了功能,但随后出现异常 TypeError: the first argument must be callable
在 styler.to_excel('Output.xlsx')
线。
最佳答案
从版本 1.3.0 开始,Pandas applymap
接受 subset
范围:
subset : label, array-like, IndexSlice, optional
A valid 2d input to DataFrame.loc[], or, in the case of a 1d input or single key, to DataFrame.loc[:, ] where the columns are prioritised, to limit data to before applying the function.
因此,例如,为了仅在 Excel 输出文件中为“Col1”着色,您可以像这样修改代码:
styler.applymap(_color_red_or_green, subset=["Col1"])
从那里,您可以定义以下函数:def colorize(df, cols):
def _color_red_or_green(val):
color = "red" if val < 0 else "green"
return "color: %s" % color
styler = df.style
styler.applymap(_color_red_or_green, subset=cols)
styler.to_excel("Output.xlsx")
然后使用您选择的数据框和列调用它:colorize(df, ["Col1", "col3"])
它输出一个 Excel 文件,其中“Col1”和“col3”值都涂成绿色。
关于python - 如何根据我的列名( Pandas )制作特定样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70399791/