0

I have this dataframe

df =
          name     age     character
0          A        10       fire
1          A        15       water
2          A        20       earth
3          A        25       air
4          B        10       fire
5          B        7        air

I organized it with groupby,

df = df.groupby('name').aggregate(list)

then I have this output

        age          character
name
---------------------------------------
A     [10,15,20,25]   [fire, water, earth, air]
B     [10, 7]         [fire, air]

I tried to pivot this dataframe, based on name column. But after groupby, name columns is not in the columns anymore

print(df.columns)
>>> ["age", "character"]

How can I lift up this column, so that I can use for pivot?
EDIT Expected output is,

name      age          character
---------------------------------------
A     [10,15,20,25]   [fire, water, earth, air]
B     [10, 7]         [fire, air]
jayko03
  • 2,329
  • 7
  • 28
  • 51

1 Answers1

2
df = df.reset_index()

However, pivoting list data is usually unhelpful.

Jim Eisenberg
  • 1,490
  • 1
  • 9
  • 17