I want to reverse a DataFrame.groupby().agg(list)
.
This is the initial data.
PERSON FOO BAR
0 Maya 12 0
1 Maya 3 1
2 Jim 9 2
3 Jim 7 3
And that is the result groupby().agg(list)
and some more work on it.
FOO BAR
PERSON
Jim [9, 7] 2 | 3
Maya [12, 3] 0 | 1
I expect a result like this
PERSON FOO BAR
0 Maya 12 0 | 1
1 Maya 3 0 | 1
2 Jim 9 2 | 3
3 Jim 7 2 | 3
In words:
- I want to "unlist" the cell content in
FOO
and - keep/duplicate the content in
BAR
.
This is a full MWE:
#!/usr/bin/env python3
import pandas as pd
df = pd.DataFrame(
{
'PERSON': ['Maya', 'Maya', 'Jim', 'Jim'],
'FOO': [12, 3, 9, 7],
'BAR': range(4)
}
)
print(df)
# Group by Person but keep all content as list's in the cells
dfg = df.groupby('PERSON').agg(list)
# Do some work on the result
dfg.BAR = dfg.BAR.apply(lambda cell: ' | '.join([str(e) for e in cell]))
print(dfg)