pandas 根据多个条件格式化熊猫数据框中单元格的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43839112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format the color of a cell in a pandas dataframe according to multiple conditions
提问by Yusef Jacobs
I am trying to format the color of a cell of an specific column in a data frame, but I can't manage to do it according to multiple conditions.
我正在尝试格式化数据框中特定列的单元格的颜色,但我无法根据多个条件进行设置。
This is my dataframe (df):
这是我的数据框(df):
Name ID Cel Date
0 Diego b000000005 7878 2565-05-31 20:53:00
1 Luis b000000015 6464 2017-05-11 20:53:00
2 Vidal b000000002 1100 2017-05-08 20:53:00
3 John b000000011 4545 2017-06-06 20:53:00
4 Yusef b000000013 1717 2017-06-06 20:53:00
I want the values in the "Date" column to change color according to the following conditions:
我希望“日期”列中的值根据以下条件更改颜色:
if date < datetime.now():
color = 'green'
elif date > datetime.now():
date = 'yellow'
elif date > (datetime.now() + timedelta(days=60)):
color = 'red'
This is my current code:
这是我当前的代码:
def color(val):
if val < datetime.now():
color = 'green'
elif val > datetime.now():
color = 'yellow'
elif val > (datetime.now() + timedelta(days=60)):
color = 'red'
return 'background-color: %s' % color
df.style.apply(color, subset = ['Fecha'])
I am getting the following error:
我收到以下错误:
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Fecha')
ValueError: ('Series 的真值不明确。使用 a.empty, a.bool(), a.item(), a.any() 或 a.all()。', 'occurred at index Fecha' )
The output is:
输出是:
Out[65]: <pandas.formats.style.Styler at 0x1e3ab8dec50>
Any help will be appreciated.
任何帮助将不胜感激。
回答by andrew_reece
Use applymap
:
使用applymap
:
from datetime import datetime, timedelta
import pandas as pd
name = ['Diego', 'Luis', 'Vidal', 'John', 'Yusef']
id = ['b000000005', 'b000000015', 'b000000002', 'b000000011', 'b000000013']
cel = [7878, 6464, 1100, 4545, 1717]
date = pd.to_datetime(['2017-05-31 20:53:00', '2017-05-11 20:53:00', '2017-05-08 20:53:00',
'2017-06-06 20:53:00', '2017-06-06 20:53:00'])
df = pd.DataFrame({'Name':name,'ID':id,'Cel':cel,'Date':date})
def color(val):
if val < datetime.now():
color = 'green'
elif val > datetime.now():
color = 'yellow'
elif val > (datetime.now() + timedelta(days=60)):
color = 'red'
return 'background-color: %s' % color
df.style.applymap(color, subset=['Date'])
Screenshot from Jupyter notebook. If you print
the output instead, you'll just get a reference to the Styler
object:
Jupyter 笔记本的屏幕截图。如果您print
改为输出,您将只获得对Styler
对象的引用:
print(df.style.applymap(color, subset=['Date']))
<pandas.formats.style.Styler object at 0x116db43d0>
回答by julia
The way it is written, no cells would be painted red. You should change the order, letting the condition to paint red before the one to paint yellow.
它的书写方式,没有单元格会被涂成红色。您应该更改顺序,让条件先涂红色,然后再涂黄色。