使用Panda的DataFrame()风格格式化日期时间索引 [英] Formatting Datetime Index with Panda's DataFrame().style

查看:2777
本文介绍了使用Panda的DataFrame()风格格式化日期时间索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Pandas DataFrames的style属性来创建HTML表格来发送电子邮件。
我遇到的问题是,我有一个日期时间索引显示为日期时间戳,而我希望它显示为一个日期。我对时间不感兴趣。在解释器中,数据框打印正确(仅显示日期部分)。但是,当使用表格的style属性进行风格化之后,它会生成HTML,以便放出时间部分。我已经看着使用style.format(),但我不能访问索引列。
我会重置索引,以使日期时间列正常列...但我的标题列是多指标。如果我变平,不使用索引表,看起来很奇怪。



不幸的是我在.style文档中找到了这个:


限制

仅适用于DataFrame(使用Series.to_frame()。style)索引和列
必须是独特的没有大的repr,而且表现不是很好;这是用于汇总数据框的
您只能对值进行样式设置,而不能对
索引或列进行样式化只能应用样式,不能插入新的HTML
实体其中的一些将会被解决在未来。



  pd.DataFrame(data,index = index.strftime(%Y-%m-%d))。style.format({:。2})


I'm using the style property of Pandas DataFrames to create HTML tables for emailing. The issue I am having is that i have a datetime index that shows up as a datetime stamp when I'd like it to show as a date instead. I'm not interested in the time part. In the interpreter, the dataframe does print out correctly (only shows the date part). But when I render after stylizing using the style property of the table, it generates HTML that puts out the time part as well. I've looked into using style.format() but I can't access the index column. I would reset the index in order to make the datetime column a normal column... but my header columns are multindex. If I flatten out and don't use indexes the table looks weird.

Unfortunately I found this in the .style documentation:

Limitations

DataFrame only (use Series.to_frame().style) The index and columns must be unique No large repr, and performance isn’t great; this is intended for summary DataFrames You can only style the values, not the index or columns You can only apply styles, you can’t insert new HTML entities Some of these will be addressed in the future.

https://pandas.pydata.org/pandas-docs/stable/style.html#Limitations

I'm posting to see if anyone has any ideas for how I can get around this. Thanks!

Example table that shows the issue: example_table_link

Code for generating table:

account_day_df = merged[['Day', 'Metric1', 'Metric2', 'Metric3', 'Metric4', 'Campaign type']]
account_day_df = account_day_df.groupby(['Day', 'Campaign type']).sum()
account_day_df.loc[:, 'Metric5'] = account_day_df['Metric1'] / account_day_df['Metric4']
account_day_df = account_day_df.unstack('Campaign type')

html += (
    account_day_df.style
        .background_gradient(cmap=cm, subset=['Metric5'])
        .set_table_attributes('border="1" class="dataframe table table-hover table-bordered"')
        .render(i)
)

解决方案

You can convert your index to object instead of datetime using strftime() by doing df.index = df.index.strftime("%Y-%d-%m"). Here's an example:

data = np.random.randn(10, 5)
index = pd.date_range('20130101', periods=10, freq='D')
pd.DataFrame(data, index=index).style.format("{:.2}")

pd.DataFrame(data, index=index.strftime("%Y-%m-%d")).style.format("{:.2}")

这篇关于使用Panda的DataFrame()风格格式化日期时间索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆