1

My question is similar to this one, however I do need renaming columns because I aggregate my data using functions:

def series(x):
    return ','.join(str(item) for item in x)

agg = {
    'revenue': ['sum', series],
    'roi': ['sum', series],
}

df.groupby('name').agg(agg)

As a result I have groups of identically named columns:

enter image description here

which become completely indistinguishable after I drop the higher column level:

df.columns = df.columns.droplevel(0)

enter image description here

So, how do I go about keeping unique names for my columns?

kurtgn
  • 8,140
  • 13
  • 55
  • 91
  • Possible duplicate of [Pandas: Is there a way to use something like 'droplevel' and in process, rename the the other level using the dropped level labels as prefix/suffix?](https://stackoverflow.com/questions/39405971/pandas-is-there-a-way-to-use-something-like-droplevel-and-in-process-rename) – Zero Oct 16 '17 at 15:29
  • and duplicate of [Pandas: use one column for groupby and get stats for multiple other columns](https://stackoverflow.com/questions/46498118/pandas-use-one-column-for-groupby-and-get-stats-for-multiple-other-columns) – Zero Oct 16 '17 at 15:31

1 Answers1

3

Use map for flatten columns names:

df.columns = df.columns.map('_'.join)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252