3

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

1 Answer 1

1

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        
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.