import numpy ...">

Expand data frames and save columns

I have a DataFrame that is in a too "compact" form. Currently, a DataFrame is as follows:

> 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 “unzip” it like this:

> 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 collapse, I can not get it right.

+4
source share
2 answers

Use the 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
+4
source

set your index to foo, then execute the stack:

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

foo     
A    bar    1
B    bar    2
     baz    3
dtype: object
+2
source

Source: https://habr.com/ru/post/1678120/


All Articles