Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a dataframe column to join values of another column after groupby

I have a dataframe like this:

import pandas as pd

df = pd.DataFrame(
    {
        'group': list('ABBCA'),
        'values': list('xyztr'),
        'joiner': ['j1', 'j2', 'j2', 'j3', 'j1']
    }
)

  group values joiner
0     A      x     j1
1     B      y     j2
2     B      z     j2
3     C      t     j3
4     A      r     j1

I now want to groupby the column group and join the respective values in values using the entry in joiner. So something like this:

df.groupby('group')['values'].transform(" - ".join)

0    x - r
1    y - z
2    y - z
3        t
4    x - r

Just that instead " - " it should use the respective value in df['joiner'].

How would I do this?

Expected outcome would be:

0    x j1 r
1    y j2 z
2    y j2 z
3        t
4    x j1 r

We can safely assume that the joiner value is consistent with the group column (otherwise the groupby would fail).

like image 581
Cleb Avatar asked Dec 03 '22 10:12

Cleb


1 Answers

here is one way:

m=df.groupby('group').agg({'values':list,'joiner':'first'})
s=pd.Series([f' {b} '.join(a) for a,b in zip(m['values'],m['joiner'])],index=m.index)
final=df.assign(new=df.group.map(s))

  group values joiner     new
0     A      x     j1  x j1 r
1     B      y     j2  y j2 z
2     B      z     j2  y j2 z
3     C      t     j3       t
4     A      r     j1  x j1 r
like image 142
anky Avatar answered May 19 '23 08:05

anky