pandas pivot_table() 按日期分多列數(shù)據(jù)的方法
如下所示:
date | 20170307 | 20170308 |
iphone4 | 2 | 0 |
iphone5 | 2 | 1 |
iphone6 | 0 | 1 |
先生成DF數(shù)據(jù)。
>>> df = pd.DataFrame.from_dict([['ip4','20170307',1],['ip4','20170307',1],['ip5','20170307',1],['ip5','20170307',1],['ip6','20170308',1],['ip5','20170308',1]]) >>> df.columns=['type','date','num'] >>>df
type date num 0 ip4 20170307 1 1 ip4 20170307 1 2 ip5 20170307 1 3 ip5 20170307 1 4 ip6 20170308 1 5 ip5 20170308 1
>>> pd.pivot_table(df,values='num',rows=['type'],cols=['date'],aggfunc=np.sum).fillna(0)
操作一下就是實現(xiàn)結(jié)果。
注:這個函數(shù)的參數(shù)形式在0.13.x版本里有效,其他版本請參考相應文檔。
從0.14.0開始,參數(shù)形式升級成pd.pivot_table(df,values='num',index=['type'],columns=['date'],aggfunc=np.sum).fillna(0)
以上這篇pandas pivot_table() 按日期分多列數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python調(diào)用chrome實現(xiàn)網(wǎng)頁自動操作過程
這篇文章主要介紹了python調(diào)用chrome實現(xiàn)網(wǎng)頁自動操作,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11解決win10 vscode 無法激活python 虛擬環(huán)境的問題
這篇文章主要介紹了win10 vscode 無法激活python 虛擬環(huán)境的解決辦法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10pandas 數(shù)據(jù)實現(xiàn)行間計算的方法
今天小編就為大家分享一篇pandas 數(shù)據(jù)實現(xiàn)行間計算的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06