如下所示:
| date | 20170307 | 20170308 |
| iphone4 | 2 | 0 |
| iphone5 | 2 | 1 |
| iphone6 | 0 | 1 |
先生成DF数据。
>>> df = pd.Data .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)
操作一下就是实现结果。
注:这个函数的参数形式在0.13.x版本里有效,其他版本请参考相应文档。
从0.14.0开始,参数形式升级成pd.pivot_table(df,values=\'num\',index=[\'type\'],columns=[\'date\'],aggfunc=np.sum).fillna(0)
以上这篇pandas pivot_table() 按日期分多列数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
你看到的最直白清晰的CNN讲解
-
多路RTSP-RTMP转RTMP官方定制版
2026-05-19栏目: 教程
-
服务器租用价格多少,决定云服务器价格的因素
2026-05-19栏目: 教程
-
企业上云的好处优势告诉你企业为什么要上云?
2026-05-19栏目: 教程
-
阿里云910会员节大促主会场全攻略
2026-05-19栏目: 教程
-
Agentless监控实践中的预警指标采集
2026-05-19栏目: 教程
