Pandas按周/月/年統(tǒng)計數(shù)據(jù)介紹
Pandas 按周、月、年、統(tǒng)計數(shù)據(jù)
介紹
將日期轉(zhuǎn)為時間格式 并設(shè)置為索引
import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['訂單創(chuàng)建時間','總金額'])
print(data)
data['訂單創(chuàng)建時間']=pd.to_datetime(data['訂單創(chuàng)建時間'])
data=data.set_index('訂單創(chuàng)建時間')
print(data)

按周、月、季度、年統(tǒng)計數(shù)據(jù)
import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['訂單創(chuàng)建時間','總金額'])
data['訂單創(chuàng)建時間']=pd.to_datetime(data['訂單創(chuàng)建時間'])
data=data.set_index('訂單創(chuàng)建時間')
print(data.resample('w').sum())
print(data.resample('m').sum())
print(data.resample('Q').sum())
print(data.resample('AS').sum())


使用to_period()方法 優(yōu)化
按月、季度和年顯示數(shù)據(jù)(不統(tǒng)計數(shù)據(jù))
import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['訂單創(chuàng)建時間','總金額'])
data['訂單創(chuàng)建時間']=pd.to_datetime(data['訂單創(chuàng)建時間'])
data=data.set_index('訂單創(chuàng)建時間')
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'))


與之前相比 日期的顯示方式發(fā)生了改變
到此這篇關(guān)于Pandas按周/月/年統(tǒng)計數(shù)據(jù)介紹的文章就介紹到這了,更多相關(guān)Pandas統(tǒng)計數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在NumPy中創(chuàng)建空數(shù)組/矩陣的方法
今天小編就為大家分享一篇在NumPy中創(chuàng)建空數(shù)組/矩陣的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
python學生信息管理系統(tǒng)實現(xiàn)代碼
這篇文章主要為大家詳細介紹了python學生信息管理系統(tǒng)的實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Python中計算三角函數(shù)之cos()方法的使用簡介
這篇文章主要介紹了Python中計算三角函數(shù)之cos()方法的使用簡介,是Python入門的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05
pandas進行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法
今天小編就為大家分享一篇pandas進行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

