pandas:如何使用多索引进行数据透视?

pandas:如何使用多索引进行数据透视?,第1张

pandas:如何使用多索引进行数据透视

您可以分组然后再堆叠

>>> df.groupby(['year', 'month', 'item'])['value'].sum().unstack('item')item        item 1  item 2year month     2004 1          33     250     2          44     224     3          41     268     4          29     232     5          57     252     6          61     255     7          28     254     8          15     229     9          29     258     10         49     207     11         36     254     12         23     209

或使用

pivot_table

>>> df.pivot_table(        values='value',         index=['year', 'month'],         columns='item',         aggfunc=np.sum)item        item 1  item 2year month     2004 1          33     250     2          44     224     3          41     268     4          29     232     5          57     252     6          61     255     7          28     254     8          15     229     9          29     258     10         49     207     11         36     254     12         23     209


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5647210.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存