【发布时间】:2022-09-27 14:35:55
【问题描述】:
我有一个嵌套字典列表,如下所示:
keywords_data=[{\'vol\': 90500,
\'cpc\': {\'currency\': \'$\', \'value\': \'4.64\'},
\'keyword\': \'coronary artery disease\',
\'competition\': 0.15,
\'trend\': [{\'month\': \'September\', \'year\': 2021, \'value\': 90500},
{\'month\': \'October\', \'year\': 2021, \'value\': 90500},
{\'month\': \'November\', \'year\': 2021, \'value\': 90500},
{\'month\': \'December\', \'year\': 2021, \'value\': 74000},
{\'month\': \'January\', \'year\': 2022, \'value\': 90500},
{\'month\': \'February\', \'year\': 2022, \'value\': 110000},
{\'month\': \'March\', \'year\': 2022, \'value\': 110000},
{\'month\': \'April\', \'year\': 2022, \'value\': 110000},
{\'month\': \'May\', \'year\': 2022, \'value\': 90500},
{\'month\': \'June\', \'year\': 2022, \'value\': 90500},
{\'month\': \'July\', \'year\': 2022, \'value\': 90500},
{\'month\': \'August\', \'year\': 2022, \'value\': 90500}]}]
我想将其转换为如下数据框
keyword month year value
coronary artery disease september 2021 90500
coronary artery disease october 2021 90500
coronary artery disease november 2021 90500
.
.
.
.
我可以使用访问元素关键字和竞争和每次点击费用
vol = []
cpc = []
for element in keywords_data:
vol.append(element[\"vol\"])
cpc.append(element[\"cpc\"][\"value\"])
但是当我尝试使用相同的方法访问趋势下的月份时,它会抛出一个错误,指出列表索引必须是切片或字符串,而不是 str。
如上所示,如何将其放入数据框中?
标签: python pandas dictionary