0

I have a df having more than 100 columns and i want to use groupby and take mean of column having 'WR' in it and 'sum' if not using agg().

df.groupby([c1,c2]).agg({ {i for i in df.columns if 'WR' in i}:'mean', {i for i in df.columns if 'WR' 
not in i}:'sum'})

Is it Possible?

Amit
  • 763
  • 1
  • 5
  • 14

2 Answers2

1

Create dictionaries by dict.fromkeys and merge them together:

d1 = dict.fromkeys([i for i in df.columns if 'WR' in i], 'mean')
d2 = dict.fromkeys([i for i in df.columns if 'WR' not in i], 'sum')

df.groupby([c1,c2]).agg({**d1, **d2})

Another similar solution:

m = df.columns.str.contains('WR')

d1 = dict.fromkeys(df.columns[m], 'mean')
d2 = dict.fromkeys(df.columns[~m], 'sum')

df.groupby([c1,c2]).agg({**d1, **d2})
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
-1
import pandas as pd

d1= dict.fromkeys([i for i in df.columns if 'WR in i],'mean') d2=dict.fromkeys([i for i in df.columns if'WR' not in i],'sum') df.groupby([c1,c2]),agg({**d1,**d2})