从嵌套字典创建数据框 [英] Create Dataframe from a nested dictionary
本文介绍了从嵌套字典创建数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从具有嵌套字典的值列表中创建一个数据框,所以这是我的数据
I am trying to create a dataframe from a list of values which has nested dictionaries So this is my data
d=[{'user': 200,
'p_val': {'a': 10, 'b': 200},
'f_val': {'a': 20, 'b': 300},
'life': 8},
{'user': 202,
'p_val': {'a': 100, 'b': 200},
'f_val': {'a': 200, 'b': 300},
'life': 8}]
我正尝试将其转换为数据框,如下所示:
i am trying to turn it into a dataframe as follows:
user new_col f_val p_val life
200 a 20 10 8
200 b 300 200 8
202 a 200 100 8
202 b 300 200 8
我查看了其他答案,但没有一个符合我的要求.我能找到的最接近的是此,但仍然无法正常工作.任何帮助将非常感激!谢谢
I looked at other answers, none of them matched my requirement. The nearest I could find was this, and still did not work me. Any help would be much appreciated! Thank you
推荐答案
尝试
df = pd.concat([pd.DataFrame(e) for e in d])
df.reset_index().rename(columns={'index': 'newcol'})
newcol user p_val f_val life
0 a 200 10 20 8
1 b 200 200 300 8
2 a 202 100 200 8
3 b 202 200 300 8
第一行创建一个DataFrame,其索引为 a
和 b
值.第二行将其列为 newcol
列.
The first line makes a DataFrame with the index being the a
and b
values. The second line makes this the newcol
column.
这篇关于从嵌套字典创建数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文