【问题标题】:append value in a nested dictionary to list and convert that list to dataframe将嵌套字典中的值附加到列表并将该列表转换为数据框
【发布时间】: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


    【解决方案1】:

    使用json_normalize

    df = pd.json_normalize(keywords_data, 'trend', ['vol', 'keyword', 'competition','cpc'])
    
    df = df.join(pd.json_normalize(df.pop('cpc')).add_prefix('cpc.'))
    print (df)
            month  year   value    vol                  keyword competition  \
    0   September  2021   90500  90500  coronary artery disease        0.15   
    1     October  2021   90500  90500  coronary artery disease        0.15   
    2    November  2021   90500  90500  coronary artery disease        0.15   
    3    December  2021   74000  90500  coronary artery disease        0.15   
    4     January  2022   90500  90500  coronary artery disease        0.15   
    5    February  2022  110000  90500  coronary artery disease        0.15   
    6       March  2022  110000  90500  coronary artery disease        0.15   
    7       April  2022  110000  90500  coronary artery disease        0.15   
    8         May  2022   90500  90500  coronary artery disease        0.15   
    9        June  2022   90500  90500  coronary artery disease        0.15   
    10       July  2022   90500  90500  coronary artery disease        0.15   
    11     August  2022   90500  90500  coronary artery disease        0.15   
    
       cpc.currency cpc.value  
    0             $      4.64  
    1             $      4.64  
    2             $      4.64  
    3             $      4.64  
    4             $      4.64  
    5             $      4.64  
    6             $      4.64  
    7             $      4.64  
    8             $      4.64  
    9             $      4.64  
    10            $      4.64  
    11            $      4.64  
    

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多