ajrlewis
ajrlewis

Reputation: 3058

Pandas groupby data frame for duplicate rows

I have the following data frame:

data = dict(t=[0, 1, 0, 1], s=[0, 31, 4, 26])
df = pd.DataFrame(data=data)

How can I use df.groupby(['t']) in order to end up with a data frame that looks like this:

t    s_0    s_1
0    0      31
1    4      26

Thanks for any help.

Upvotes: 1

Views: 66

Answers (1)

jezrael
jezrael

Reputation: 862471

Idea is create for each group new row with GroupBy.apply and then reshape by Series.unstack with first level, last some data cleaning:

df1 = (df.groupby('t')['s']
         .apply(lambda x: pd.Series(x.to_numpy()))
         .unstack(0)
         .add_prefix('s_')
         .rename_axis(index='t', columns=None)
         .reset_index()
)
print (df1)
   t  s_0  s_1
0  0    0   31
1  1    4   26

Upvotes: 1

Related Questions