在多索引数据帧熊猫中聚合

拉兹洛帕纳弗莱克斯

我正在寻找有关此多索引数据框的帮助

import numpy as np
import pandas as pd

array = [np.array(['jan','jan','feb','feb','mar','mar']), np.array(['food','rent','food','rent','food','rent'])]

df = pd.DataFrame(np.random.randint(0,high=100,size=(6,1)),index=array,columns=['expense'])

我试图总结整个数据帧中“食物”和“租金”的数量 - 最简单的方法是什么?

西多姆

您可以groupby索引级别(food并且rent处于级别 1 索引):

df.groupby(level=1).sum()

#      expense
#food      166
#rent      161

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章