Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas groupby and apply function on multiple columns

If I have a function f that I am applying to more than once to a set of columns, what's a more Pythonic way of going about it. Right now, what I am doing is this.

newdf=df.groupby(['a', 'b']).apply(lambda x: f(x, 1))
newdf.columns=['1']
newdf['2']=df.groupby(['a', 'b']).apply(lambda x: f(x, 2))
newdf['3']=df.groupby(['a', 'b']).apply(lambda x: f(x, 3))
newdf['4']=df.groupby(['a', 'b']).apply(lambda x: f(x, 4))

Is there a better way of going about it?

Thanks,

like image 510
Linda Avatar asked Oct 16 '22 17:10

Linda


2 Answers

That's pythonic enough for me:

columns_dict = dict()
for i in range(1, 5):
    columns_dict[str(i)] = df.groupby(["a", "b"]).apply(lambda x: f(x, i))

pd.DataFrame(columns_dict)
like image 93
koPytok Avatar answered Oct 20 '22 23:10

koPytok


You could do :

pandas.DataFrame([df.groupby(['a','b']).apply(lambda x : f(x,i)) for i in range(1,5)])

Then transpose the new DataFrame if you want to have same column names as the initial dataframe.

like image 38
Neroksi Avatar answered Oct 21 '22 00:10

Neroksi