Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unstack dataframe and keep columns

I have a DataFrame that is in a too much "compact" form. The DataFrame is currently like this :

> import numpy as np
> import pandas as pd

> df = pd.DataFrame({'foo': ['A','B'],
               'bar': ['1', '2'],
               'baz': [np.nan, '3']})
  bar  baz foo
0   1  NaN   A
1   2    3   B

And I need to "unstack" it to be like so :

> df = pd.DataFrame({'foo': ['A','B', 'B'],
               'type': ['bar', 'bar', 'baz'],
               'value': ['1', '2', '3']})

  foo type value
0   A  bar     1
1   B  bar     2
2   B  baz     3

No matter how I try to pivot, I can't get it right.

like image 219
Rémi Avatar asked Feb 05 '23 09:02

Rémi


2 Answers

Use melt() method:

In [39]: pd.melt(df, id_vars='foo', value_vars=['bar','baz'], var_name='type')
Out[39]:
  foo type value
0   A  bar     1
1   B  bar     2
2   A  baz   NaN
3   B  baz     3

or

In [38]: pd.melt(df, id_vars='foo', value_vars=['bar','baz'], var_name='type').dropna()
Out[38]:
  foo type value
0   A  bar     1
1   B  bar     2
3   B  baz     3
like image 183
MaxU - stop WAR against UA Avatar answered Feb 08 '23 08:02

MaxU - stop WAR against UA


set your index to foo, then stack:

df.set_index('foo').stack()

foo     
A    bar    1
B    bar    2
     baz    3
dtype: object
like image 24
Steven G Avatar answered Feb 08 '23 06:02

Steven G