How to unnest cells in a DataFrame, employing pandas and python? -
suppose have dataframe
df:
a b c v f 3|4|5 v 2 6 v f 4|5
i'd produce 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 make transformation in r, using tidyr
package. there easy way of doing in pandas?
you could:
import numpy np df = df.set_index(['a', 'b']) df = df.astype(str) + '| ' # there's space ' ' match replace later df = df.c.str.split('|', expand=true).stack().reset_index(-1, drop=true).replace(' ', np.nan).dropna().reset_index() # , replace has space ' '
to get:
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
Comments
Post a Comment