0

When I try to get more than one aggregation for a DataFrame, I get a header with more than one row:

df = df.groupby(['user', 'cat1 'cat2'])

df = df.aggregate({
    'user': ['count'],
    'profit': [np.average, np.sum]
    })

Output:

user                                profit
                        count   average    sum
user    cat1    cat2            
123     A       85963   1       91.33    56.22
456     A       5639    1       67.32    52.11
789     B       68593   1       33.55    45.88

Exporting this to csv results in two or more header rows:

,user,cat1,cat2,user,profit,profit
,,,,count,average,sum
[...]

What am I doing wrong?

n.r.
  • 831
  • 1
  • 11
  • 30
  • 1
    It is `MultiIndex`, in duped answers are possible solutions for handle it - need flatten it. – jezrael May 10 '21 at 12:29
  • 1
    @jezrael Indeed it is, "MultiIndex" seems to be the term I was missing here, thanks a lot! – n.r. May 10 '21 at 13:57

0 Answers0