Pandas按周/月/年统计数据介绍

发布时间:2022-04-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Pandas按周/月/年统计数据介绍脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Pandas 按周、月、年、统计数据

介绍

将日期转为时间格式 并设置为索引

import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['订单创建时间','总金额'])
PRint(data)
data['订单创建时间']=pd.to_datetime(data['订单创建时间'])
data=data.set_index('订单创建时间')
print(data)

image-20211212113513921

按周、月、季度、年统计数据

import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['订单创建时间','总金额'])
data['订单创建时间']=pd.to_datetime(data['订单创建时间'])
data=data.set_index('订单创建时间')
print(data.resample('w').sum())
print(data.resample('m').sum())
print(data.resample('Q').sum())
print(data.resample('AS').sum())

image-20211212113905454

image-20211212113915052

使用to_PEriod()方法 优化

按月、季度和年显示数据(不统计数据)

import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['订单创建时间','总金额'])
data['订单创建时间']=pd.to_datetime(data['订单创建时间'])
data=data.set_index('订单创建时间')
print(data.resample('w').sum().to_period('w'))
print(data.resample('m').sum().to_period('m'))
print(data.resample('q').sum().to_period('q'))
print(data.resample('as').sum().to_period('a'))

image-20211212114219970

image-20211212114235410

与之前相比 日期的显示方式发生了改变

到此这篇关于Pandas按周/月/年统计数据介绍的文章就介绍到这了,更多相关Pandas统计数据内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本宝典!

脚本宝典总结

以上是脚本宝典为你收集整理的Pandas按周/月/年统计数据介绍全部内容,希望文章能够帮你解决Pandas按周/月/年统计数据介绍所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: