1

I have a data frame like following

id,  values
1, {'foo':2 , 'bar':2}
2, {'baz':20}
and so on

I want to transform this dataframe into

id val1 val2
1  foo    2
1 bar     2
2 baz     20

and so on.. I dont want to like iterate each row in dataframe.. as I am pretty sure there is a way in pandas to do the above?

1 Answer 1

2

IIUC PS: you can add rename at the end

df.set_index('id')['values'].apply(pd.Series).stack().reset_index()
Out[920]: 
   id level_1     0
0   1     bar   2.0
1   1     foo   2.0
2   2     baz  20.0
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.