cybharg
cybharg

Reputation: 55

Convert column with nested lists into columns with first element as column name

I have a dataframe with nested lists in one of the column. Need to flatten the lists out with the first element in each nested list as the column name.

df:

Index  |  list_col
1      |  [['x', 'a1'],['y', 'b2'],['z', 'c3']]
2      |  [['x', 'a4'],['y', 'b5']]
3      |  [['x', 'a6'],['y', 'b7'],['z', 'c8']]
4      |  [['x', 'a9']]

I am able to use this and convert each nested list into columns -

df_flat = pd.DataFrame(df.list_col.values.tolist(), df.index, dtype=object).fillna('')

Index   |0             |1           |2
1       |['x', 'a1']   |['y', 'b2'] |['z', 'c3']
2       |['x', 'a4']   |['y', 'b5'] |
3       |['x', 'a6']   |['y', 'b7'] |['z', 'c8']
4       |['x', 'a9']    

What I want to acheive:

Index |x  |y  |z
1     |a1 |b2 |c3
2     |a4 |b5 |
3     |a6 |b7 |c8
4     |a9 |   |

Upvotes: 3

Views: 163

Answers (1)

jezrael
jezrael

Reputation: 862601

Use list comprehension with converting to dictionary and then DataFrame constructor:

df = pd.DataFrame([dict(x) for x in df.list_col], index=df.index).fillna('')
print (df)
        x   y   z
Index            
1      a1  b2  c3
2      a4  b5    
3      a6  b7  c8
4      a9        

Upvotes: 1

Related Questions