如何在groupby中获取重复的主键?

时间:2017-08-04 11:56:17

标签: python pandas

每当我使用groupby时,我得到的结果如下:

 df.groupby(by =['day','gender']).['total].sum()

输出:

gender day  sum
male   mon   56
       tues  45
       ..
female mon    44
       tues   25
        ...   

我希望输出:

gender day  sum
male   mon   56
male   tues  45
       ..
female mon    44
female tues   25

1 个答案:

答案 0 :(得分:1)

try:

df.groupby(by=['day', 'gender'], as_index=False).['total'].sum()