Python - Unnest cells in Pandas DataFrame

Suppose I have DataFrame df:

a b c
v f 3|4|5
v 2 6
v f 4|5

I would like to create this df:

a b c
v f 3
v f 4
v f 5
v 2 6
v f 4
v f 5

I know how to do this conversion to R using a package tidyr.

Is there an easy way to do this in pandas?

+4
source share
2 answers

You can:

import numpy as np

df = df.set_index(['a', 'b'])
df = df.astype(str) + '| ' # There a space ' ' to match the replace later
df = df.c.str.split('|', expand=True).stack().reset_index(-1, drop=True).replace(' ', np.nan).dropna().reset_index() # and replace also has a space ' '

To obtain:

   a  b  0
0  v  f  3
1  v  f  4
2  v  f  5
3  v  2  6
4  v  f  4
5  v  f  5
+2
source

Option 1

In [3404]: (df.set_index(['a', 'b'])['c']
              .str.split('|', expand=True).stack()
              .reset_index(name='c').drop('level_2', 1))
Out[3404]:
   a  b  c
0  v  f  3
1  v  f  4
2  v  f  5
3  v  2  6
4  v  f  4
5  v  f  5

Option 2 Use repeatandloc

In [3503]: s = df.c.str.split('|')

In [3504]: df.loc[df.index.repeat(s.str.len())].assign(c=np.concatenate(s))
Out[3504]:
   a  b  c
0  v  f  3
0  v  f  4
0  v  f  5
1  v  2  6
2  v  f  4
2  v  f  5

More details

In [3505]: s
Out[3505]:
0    [3, 4, 5]
1          [6]
2       [4, 5]
Name: c, dtype: object
0
source

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


All Articles