Skip to content Skip to sidebar Skip to footer

Reshaping A Melted Pandas Dataframe

I'm trying to reshape a melted dataframe from another question. As of now, this is what I have: col1 variable value 0 A col2 1 1 B col2 2 2 A c

Solution 1:

One way using groupby

In [589]: (df.groupby(['col1', 'variable']).value.apply(list)
             .apply(pd.Series)
             .rename(columns=lambda x: 'value{}'.format(x+1))
             .reset_index())
Out[589]:
  col1 variable  value1  value2
0    A     col2       1       3
1    A     col3       5       6
2    B     col2       2       3
3    B     col3       4       5

Post a Comment for "Reshaping A Melted Pandas Dataframe"