如何将条件颜色样式应用于 pandas 数据框中的列
How to apply conditional color stying to a column in a pandas dataframe
我正在尝试将样式器函数应用于 df 但出现此错误,我不知道如何修复它。
ValueError: Function <function highlight at 0x0000029D89935F30> resulted in the apply method collapsing to a Series.
Usually, this is the result of the function returning a single value, instead of list-like.
我有一个如下所示的数据框
Value Limit Actual
Issues < 33 0
Rating > 4 4.2
Complaints < 15 18
Time 30 - 45 41
Tip -- -
我想根据 Limit
列为 Actual
列着色。
例如,如果 Limit
有“< 33”,如果 Actual
列中的值小于 30,我想将 Actual
着色为绿色,如果不是红色。其他行也类似。
如果 Limit
有“30-45”,如果 Actual
列中的值介于 30 和 45 之间,我想将 Actual
着色为绿色, 如果不是红色。
如果 Limit
有“-”,我想将 Actual
颜色设为黄色。
基于这个 SO 问题 ,我创建了一个计算逻辑的函数。
import re
def highlight(row):
numbers = re.findall('[0-9]+', row['Limit'])
if row['Value'] in ('Issues','Rating','Complaints'):
if '>' in row['Limit'] and row['Actual'] > numbers[0]:
color = 'green'
elif row['Actual'] < numbers[0]:
color = 'green'
else:
color = 'red'
else:
if len(numbers) == 0:
color = 'yellow'
elif row['Actual'] > numbers[0] and row['Actual'] < numbers[1]:
color = 'green'
else:
color = 'red'
return f"background-color: {color}"
但是,当我尝试 df.style.apply(highlight, axis=1)
时,我在问题的开头收到了错误消息。
如何正确实现我的逻辑以获得我想要的着色?
当我将这个 df 导出到 excel.
时,下面是我想要的
错误消息指出了问题:“通常,这是函数 return 单个值而不是 list-like 的结果。”您有多个列,但您只 return 设置其中一个的样式,df.style.apply()
不知道是哪一个。 The docs explain:
func
should take a Series if axis
in [0,1] and return a list-like object of same length, or a Series, not necessarily of same length, with valid index labels considering subset
.
...
The elements of the output of func
should be CSS styles as strings, in the format 'attribute: value; attribute2: value2; ...' or, if nothing is to be applied to that element, an empty string or None
.
所以在这种情况下,您可以简单地 return 一个没有为普通列指定样式的列表:
return [None, None, f"background-color: {color}"]
或者您可以 return 一个仅指定“实际”列样式的系列。即使您重新排列列或添加更多列,这仍然有效。
return pd.Series({'Actual': f"background-color: {color}"})
(旁注:我很惊讶它不允许听写,即 return {'Actual': f"background-color: {color}"}
。)
我正在尝试将样式器函数应用于 df 但出现此错误,我不知道如何修复它。
ValueError: Function <function highlight at 0x0000029D89935F30> resulted in the apply method collapsing to a Series.
Usually, this is the result of the function returning a single value, instead of list-like.
我有一个如下所示的数据框
Value Limit Actual
Issues < 33 0
Rating > 4 4.2
Complaints < 15 18
Time 30 - 45 41
Tip -- -
我想根据 Limit
列为 Actual
列着色。
例如,如果
Limit
有“< 33”,如果Actual
列中的值小于 30,我想将Actual
着色为绿色,如果不是红色。其他行也类似。如果
Limit
有“30-45”,如果Actual
列中的值介于 30 和 45 之间,我想将Actual
着色为绿色, 如果不是红色。如果
Limit
有“-”,我想将Actual
颜色设为黄色。
基于这个 SO 问题
import re
def highlight(row):
numbers = re.findall('[0-9]+', row['Limit'])
if row['Value'] in ('Issues','Rating','Complaints'):
if '>' in row['Limit'] and row['Actual'] > numbers[0]:
color = 'green'
elif row['Actual'] < numbers[0]:
color = 'green'
else:
color = 'red'
else:
if len(numbers) == 0:
color = 'yellow'
elif row['Actual'] > numbers[0] and row['Actual'] < numbers[1]:
color = 'green'
else:
color = 'red'
return f"background-color: {color}"
但是,当我尝试 df.style.apply(highlight, axis=1)
时,我在问题的开头收到了错误消息。
如何正确实现我的逻辑以获得我想要的着色? 当我将这个 df 导出到 excel.
时,下面是我想要的错误消息指出了问题:“通常,这是函数 return 单个值而不是 list-like 的结果。”您有多个列,但您只 return 设置其中一个的样式,df.style.apply()
不知道是哪一个。 The docs explain:
func
should take a Series ifaxis
in [0,1] and return a list-like object of same length, or a Series, not necessarily of same length, with valid index labels consideringsubset
....
The elements of the output of
func
should be CSS styles as strings, in the format 'attribute: value; attribute2: value2; ...' or, if nothing is to be applied to that element, an empty string orNone
.
所以在这种情况下,您可以简单地 return 一个没有为普通列指定样式的列表:
return [None, None, f"background-color: {color}"]
或者您可以 return 一个仅指定“实际”列样式的系列。即使您重新排列列或添加更多列,这仍然有效。
return pd.Series({'Actual': f"background-color: {color}"})
(旁注:我很惊讶它不允许听写,即 return {'Actual': f"background-color: {color}"}
。)