![](/img/trans.png)
[英]How can I traverse a deeply nested dictionary which has lists and other dictionaries within it in Python?
[英]How do I convert a dictionary that has nested dictionaries within it into a dataframe in Python?
我最近在 Python 中使用 Oracle 的 AI 语言 API 进行了情绪分析。 我让 API 迭代超过 1300 条推文,并将 API 中的 output 存储在一个列表中,其中列表中的每个元素 ID 推文对应一个推文。 然后我创建了一个字典,其中键是推文 ID,值是来自该推文 ID 的 API 的 output。 我现在有一个庞大的字典,其中字典嵌套在字典中,我不确定如何将其转换为 Pandas 中的 dataframe。
这是我正在使用的字典的前几个条目。
{1292750633104289792: {
"aspects": []
},
1275918779831238656: {
"aspects": []
},
1293251961031204865: {
"aspects": [
{
"length": 8,
"offset": 51,
"scores": {
"Negative": 0.18023298680782318,
"Neutral": 0.0,
"Positive": 0.8197670578956604
},
"sentiment": "Positive",
"text": "building"
}
]
},
1293312774563606531: {
"aspects": []
},
1293375754751881217: {
"aspects": [
{
"length": 4,
"offset": 5,
"scores": {
"Negative": 0.9987309575080872,
"Neutral": 0.0012690634466707706,
"Positive": 0.0
},
"sentiment": "Negative",
"text": "poll"
}
]
}}
提前非常感谢。
您可以使用嵌套理解来展平您的结构,然后将结果传递给pd.DataFrame
:
import pandas as pd
data = {1292750633104289792: {'aspects': []}, 1275918779831238656: {'aspects': []}, 1293251961031204865: {'aspects': [{'length': 8, 'offset': 51, 'scores': {'Negative': 0.18023298680782318, 'Neutral': 0.0, 'Positive': 0.8197670578956604}, 'sentiment': 'Positive', 'text': 'building'}]}, 1293312774563606531: {'aspects': []}, 1293375754751881217: {'aspects': [{'length': 4, 'offset': 5, 'scores': {'Negative': 0.9987309575080872, 'Neutral': 0.0012690634466707706, 'Positive': 0.0}, 'sentiment': 'Negative', 'text': 'poll'}]}}
r = [{'tweet_id':a,
'length':i['length'],
'offset':i['offset'],
**{f'score_{j}':k for j, k in i['scores'].items()},
'sentiment':i['sentiment'],
'text':i['text'],
}
for a, b in data.items() for i in b['aspects']]
df = pd.DataFrame(r)
Output:
tweet_id length offset score_Negative score_Neutral score_Positive sentiment text
0 1293251961031204865 8 51 0.180233 0.000000 0.819767 Positive building
1 1293375754751881217 4 5 0.998731 0.001269 0.000000 Negative poll
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.