0

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)
buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 2
    have you tried `exploding` your df ? https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.explode.html – Epsi95 Jul 21 '21 at 12:40
  • Yes it is it! It is up to you to post this as an answer. – buhtz Jul 21 '21 at 12:43

1 Answers1

1

This is it:

dfg.explode('FOO')
j__carlson
  • 1,346
  • 3
  • 12
  • 20