split dataframe column including list of lists on multiple rows into pandas -
i have dataframe df:
import pandas pd df = pd.dataframe([ [[[3,0.5, 0.4, 0.7, 5],[2, 0.5, 1, 0.8, 2],[1, 0.5, 1, 1, 2]], 'b'], [[[1, 0.5, 0.6, 0.01, 1],[2, 0.5, 0.3, 0.2, 3],[1, 0.8, 1.0, 0.04, 3]], 'd']], index = ['row1', 'row2'], columns=['col1', 'col2'])
i split col1, including list of lists, on multiple lines follows:
col1 col2 row1 [3,0.5, 0.4, 0.7, 5] b row1 [2, 0.5, 1, 0.8, 2] b row1 [1, 0.5, 1, 1, 2] b row2 [1, 0.5, 0.6, 0.01, 1] d row2 [2, 0.5, 0.3, 0.2, 3] d row2 [1, 0.8, 1.0, 0.04, 3] d
and next split col1 in 2 columns, retaining second , third elements
new_col1 new_col2 col2 row1 0.5 0.4 b row1 0.5 1 b row1 0.5 1 b row2 0.5 0.6 d row2 0.5 0.3 d row2 0.8 1.0 d
how can done make using pandas?
for first step there may not better loop:
df2 = pd.dataframe() row in df.index: col = df.ix[row, 'col1'] n = len(col) df2 = df2.append(pd.dataframe( [[c, df.ix[row, 'col2']] c in col], index=[row] * n, columns = ['col1', 'col2']))
for second step, add new columns , delete original one:
df3 = df2.copy() df3['new_col1'] = [c[1] c in df3['col1']] df3['new_col2'] = [c[2] c in df3['col1']] del df3['col1']
Comments
Post a Comment