将“所有”行添加到熊猫分组依据

时间:2019-09-23 19:37:17

标签: pandas pandas-groupby

这是我的代码(使用熊猫0.19.2)

import pandas as pd


data=StringIO("""category,region,sales
fruits,east,12
vegatables,east,3
fruits,west,5
vegatables,wst,7
""")
df = pd.read_csv(data)
print(df.groupby('category', as_index=False).agg({'sales': sum}))

这是输出:

     category  sales
0      fruits     17
1  vegatables     10

我的问题是:如何添加“全部”行,以便输出看起来像这样:

     category  sales
0      fruits     17
1  vegatables     10
          all     27

2 个答案:

答案 0 :(得分:1)

您可以尝试pivot_table并更改新数据:

new_df = df.pivot_table(columns='category',index='region', values='sales')

new_df['all'] = new_df.sum(1)

输出:

category  fruits  vegatables  all
region                           
east          12           3   15
west           5           7   12

如果您想要原始数据:

new_df.stack().to_frame(name='Sales').reset_index()

输出:

  region    category  Sales
0   east      fruits     12
1   east  vegatables      3
2   east         all     15
3   west      fruits      5
4   west  vegatables      7
5   west         all     12

答案 1 :(得分:0)

这就是我最终要做的事情:

from io import StringIO

import pandas as pd

data = StringIO("""category,region,sales
fruits,east,12
vegatables,east,3
fruits,west,5
vegatables,wst,7
""")
df = pd.read_csv(data)
body=df.groupby('category', as_index=False).agg({'sales': sum})

head=df.groupby(lambda x: True, as_index=False) #advanced panda trickery
head=head.agg({'sales': sum})
head.insert(0,'category','*all*')

print(body.append(head))

基本上,使用“ all ”行并连接创建另一个数据框