从python字典中删除数字使信息成为要存储在spark dataframe中的所有数据的父级

2024-05-03 15:39:23 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以从下面的数据中删除作为父项(0,1,2)的数字,并将“信息”设置为字典中的父项

我将api中的结果集作为字典:

    {0:{
'information': [{
    'created': '2020-10-26T00:00:00+00:00',
    'title': 'Random1',
    'published': 'YES',
}, {
    'created': '2020-11-06T00:00:00+00:00',
    'title': 'Random2',
    'published': 'YES',
}, {
    'created': '2020-10-27T00:00:00+00:00',
    'title': 'Random3',
    'published': 'YES',
}, {
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random4',
    'published': 'YES',
}]
},
{1:{
'information': [{
    'created': '2020-10-26T00:00:00+00:00',
    'title': 'Random5',
    'published': 'YES',
}, {
    'created': '2020-11-06T00:00:00+00:00',
    'title': 'Random6',
    'published': 'YES',
}, {
    'created': '2020-10-27T00:00:00+00:00',
    'title': 'Random7',
    'published': 'YES',
}, {
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random8',
    'published': 'YES',
}]
},
{2:{
'information': [{
    'created': '2020-10-26T00:00:00+00:00',
    'title': 'Random9',
    'published': 'YES',
}, {
    'created': '2020-11-06T00:00:00+00:00',
    'title': 'Random10',
    'published': 'YES',
}, {
    'created': '2020-10-27T00:00:00+00:00',
    'title': 'Random11',
    'published': 'YES',
}, {
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random12',
    'published': 'YES',
}]
}

我想删除数字0、1、2,并使“信息”成为信息的父级,其下有创建的、标题的、已发布的值,如:

{
'information': [{
    'created': '2020-10-26T00:00:00+00:00',
    'title': 'Random1',
    'published': 'YES',
}, {
    'created': '2020-11-06T00:00:00+00:00',
    'title': 'Random2',
    'published': 'YES',
}, {
    'created': '2020-10-27T00:00:00+00:00',
    'title': 'Random3',
    'published': 'YES',
}, {
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random4',
    'published': 'YES',
},{
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random5',
    'published': 'YES',
},{
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random6',
    'published': 'YES',
},{
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random7',
    'published': 'YES',
},{
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random8',
    'published': 'YES',
},{
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random9',
    'published': 'YES',
},{
    'created': '2020-10-29T00:00:00+00:00',
    'title': 'Random10',
    'published': 'YES',
}]
}

我之所以要这样做,是因为我想存储创建的、标题&;发布到数据框中,如下所示:

spark.createDataFrame(json_data['results']).show()

是否有可能做到这一点,或者是否有更好的方法将数据存储到数据帧中

编辑:

输入json的创建是在检索响应并将其分配给字典后,通过标题数组循环进行的,如:

  json_data={}

  title = ["Random1", "Random2","Random3"]

  for i in range(len(title)):
      response = requests.post(url, data=payload_json)
      json_data[i] = response.json()

Tags: 数据信息json标题data字典informationtitle
1条回答
网友
1楼 · 发布于 2024-05-03 15:39:23

也许这个脚本符合条件

combined_data={"information":[]}
title = ["Random1", "Random2","Random3"]

for i in range(len(title)):
    response = requests.post(url, data=payload_json)
    json_data_tmp = response.json()
    for v in json_data_tmp.values():
        if type(v)!=dict:
            continue
        combined_data["information"]+=v["information"]

相关问题 更多 >