Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass the current group as argument in a groupby?

Tags:

python

pandas

Consider this example

df = pd.DataFrame({'group' : ['a','a','a','b','b'],
                   'value' : [1,2,3,10,20]})

Out[39]: 
  group  value
0     a      1
1     a      2
2     a      3
3     b     10
4     b     20

Essentially I would like to group by group and be able to call a function that uses the value of the current group (a or b in this example) as an argument. That is, my function takes as input the current value and its corresponding group.

Something like

def myfunc(mygroup, myvalue):
    return myvalue.astype(str) + mygroup

df.groupby('group').value.apply(lambda x: myfunc(mygroup,x))

Which does not work. Expected output is:

Out[38]: 
0     1a
1     2a
2     3a
3    10b
4    20b

Any ideas? Thanks!

like image 588
ℕʘʘḆḽḘ Avatar asked Jan 02 '23 19:01

ℕʘʘḆḽḘ


1 Answers

You can try grouping the dataframe and adding the specific columns in groupby function

df.groupby(['group']).apply(lambda x: x['value'].astype(str) + x['group'])

Out:

group   
a      0     1a
       1     2a
       2     3a
b      3    10b
       4    20b
dtype: object

or

you can also try of looping the groups individually and adding the group to value

pd.concat([g['value'].astype(str)+i for i,g in df.groupby(['group'])])

Out:

0     1a
1     2a
2     3a
3    10b
4    20b
Name: value, dtype: object
like image 63
Naga kiran Avatar answered Jan 15 '23 19:01

Naga kiran