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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With