[英]how to create multiple rows and columns from a dictionary value in pandas
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? 依此类推。.我不想在数据框中重复每一行。.我很确定熊猫中有一种方法可以执行上述操作?
IIUC PS: you can add rename
at the end IIUC PS:您可以在末尾添加rename
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.