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 | |