Format the color of a cell in a pandas dataframe according to multiple conditions(根据多个条件格式化 pandas 数据框中单元格的颜色)
问题描述
我正在尝试格式化数据框中特定列的单元格的颜色,但我无法根据多种条件来做到这一点.
这是我的数据框 (df):
姓名 ID Cel 日期0 迭戈 b000000005 7878 2565-05-31 20:53:001 路易斯 b000000015 6464 2017-05-11 20:53:002 维达尔 b000000002 1100 2017-05-08 20:53:003 约翰 b000000011 4545 2017-06-06 20:53:004 优素福 b000000013 1717 2017-06-06 20:53:00我希望日期"列中的值根据以下条件改变颜色:
如果日期 <日期时间.now():颜色='绿色'删除日期>日期时间.now():日期 = '黄色'删除日期>(datetime.now() + timedelta(days=60)):颜色='红色'这是我当前的代码:
定义颜色(val):如果 val <日期时间.now():颜色='绿色'elif val >日期时间.now():颜色='黄色'elif val >(datetime.now() + timedelta(days=60)):颜色='红色'返回'背景颜色:%s'%颜色df.style.apply(颜色,子集 = ['Fecha'])我收到以下错误:
<块引用>ValueError: ('一个Series的真值不明确.使用a.empty、a.bool()、a.item()、a.any()或a.all().','发生在索引 Fecha')
输出是:
Out[65]: <pandas.formats.style.Styler at 0x1e3ab8dec50>任何帮助将不胜感激.
使用
Jupyter 笔记本的屏幕截图.如果您改为 print 输出,您将获得对 Styler 对象的引用:
print(df.style.applymap(color, subset=['Date']))<pandas.formats.style.Styler 对象位于 0x116db43d0>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):
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')
The output is:
Out[65]: <pandas.formats.style.Styler at 0x1e3ab8dec50>
Any help will be appreciated.
Use 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:
print(df.style.applymap(color, subset=['Date']))
<pandas.formats.style.Styler object at 0x116db43d0>
这篇关于根据多个条件格式化 pandas 数据框中单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:根据多个条件格式化 pandas 数据框中单元格的颜色
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
