[英]Issue in conversion of nested dictionary to dataframe pandas python
我有嵌套字典形式的数据,如下所示
data = {
"policy": {
"1": {
"ID": "ML_0",
"URL": "www.a.com",
"Text": "my name is Martin and here is my code"
},
"2": {
"ID": "ML_1",
"URL": "www.b.com",
"Text": "my name is Mikal and here is my code"
}
}
}
我正在尝试使用以下代码将其转换为一种数据框。
for policies in data['policy']:
new = pd.DataFrame.from_dict(data['policy'], orient='index')
我明白了
ID URL Text
1 ML_0 www.a.com my name is Martin and here is my code
2 ML_1 www.b.com my name is Mikal and here is my code
实际上,在将字典转换为数据框时,我不希望包含数字 1 和 2 的第一列。 任何人都可以帮助我,请。 谢谢
使用set_index
for policies in data['policy']:
new = pd.DataFrame.from_dict(data['policy'], orient='index').set_index('ID')
有关更多信息和选项,请参阅以下文档
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.