4

Code

import pandas as pd
df = pd.DataFrame({'A': [1, 1, 1, 2, 2],
                    'B': range(5),
                    'C': range(5)})

df1 = df.groupby('A').B.agg({'B': ['count','nunique'],'C': ['sum','median']})
df1.columns = ["_".join(x) for x in df1.columns.ravel()]

df1 Output

   B_count  B_nunique  C_sum  C_median
A                                     
1        3          3      3       1.0
2        2          2      7       3.5

Warning

__main__:1: FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version

This was the recommended way to groupby and rename till Pandas 0.20. What is the elegant way to achieve the same df1 output without this warning?

GeorgeOfTheRF
  • 8,244
  • 23
  • 57
  • 80
  • https://stackoverflow.com/questions/44635626/pandas-aggregation-warning-futurewarning-using-a-dict-with-renaming-is-depreca – BENY Dec 06 '17 at 03:49

1 Answers1

7

As @Wen stated in question comments, remove the '.B' before agg, and your dictionary inside of agg is proper. Next, you can use map and join to flatten that multiindex columns.

import pandas as pd
df = pd.DataFrame({'A': [1, 1, 1, 2, 2],
                    'B': range(5),
                    'C': range(5)})

df1 = df.groupby('A').agg({'B': ['count','nunique'],'C': ['sum','median']})
df1.columns = df1.columns.map('_'.join)

Output:

   B_count  B_nunique  C_sum  C_median
A                                     
1        3          3      3       1.0
2        2          2      7       3.5
Scott Boston
  • 147,308
  • 15
  • 139
  • 187