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

按周、月、季度、年統(tǒng)計(jì)數(shù)據(jù)
import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['訂單創(chuàng)建時(shí)間','總金額'])
data['訂單創(chuàng)建時(shí)間']=pd.to_datetime(data['訂單創(chuàng)建時(shí)間'])
data=data.set_index('訂單創(chuàng)建時(shí)間')
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)計(jì)數(shù)據(jù))
import pandas as pd
data=pd.read_excel('5\TB201812.xls',usecols=['訂單創(chuàng)建時(shí)間','總金額'])
data['訂單創(chuàng)建時(shí)間']=pd.to_datetime(data['訂單創(chuàng)建時(shí)間'])
data=data.set_index('訂單創(chuàng)建時(shí)間')
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)計(jì)數(shù)據(jù)介紹的文章就介紹到這了,更多相關(guān)Pandas統(tǒng)計(jì)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
快速解決cv2.imread()讀取圖像為BGR的問(wèn)題
這篇文章主要介紹了快速解決cv2.imread()讀取圖像為BGR的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python學(xué)習(xí)之while 循環(huán)語(yǔ)句
這篇文章主要給大家介紹了關(guān)于Python中while循環(huán)語(yǔ)句的相關(guān)資料,使用while循環(huán)語(yǔ)句可以解決程序中需要重復(fù)執(zhí)行的操作,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
在NumPy中創(chuàng)建空數(shù)組/矩陣的方法
今天小編就為大家分享一篇在NumPy中創(chuàng)建空數(shù)組/矩陣的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
python學(xué)生信息管理系統(tǒng)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了python學(xué)生信息管理系統(tǒng)的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Python中計(jì)算三角函數(shù)之cos()方法的使用簡(jiǎn)介
這篇文章主要介紹了Python中計(jì)算三角函數(shù)之cos()方法的使用簡(jiǎn)介,是Python入門(mén)的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
pandas進(jìn)行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法
今天小編就為大家分享一篇pandas進(jìn)行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

