使用重复的key:value对解析JSON到数据框

时间:2018-08-05 07:43:35

标签: python json beautifulsoup

我正在使用Eventful API,该API返回一个JSON,其中包含一堆有关事件的键和值。 JSON的简化版本(删除了我不使用的key:value对):

{'events': {'event': [{'all_day': '0',
    'calendar_count': None,
    'calendars': None,
    'categories': {'category': [
    {'id': 'books', 'name': 'Literary & Books'},
    {'id': 'attractions', 'name': 'Museums & Attractions'},
    {'id': 'community', 'name': 'Neighborhood'}]},
    'title': 'Totally Random Book Club @ Lionel Bowen Library'},
{'all_day': '0',
    'calendar_count': None,
    'calendars': None,
    'categories': {'category': [
    {'id': 'books', 'name': 'Literary & Books'},
    {'id': 'business', 'name': 'business'}]},
    'title': 'ABCs of Business Management'}
                         ]
               }
    }

因此,每个“标题”可以对应于多个“ id”键:值对。我想做的是能够将此信息放入如下所示的数据框中(不必完全如下所示,只需要一种将标题分组为多个“ id”的方法):

enter image description here

我尝试使用下面的代码进行此操作,问题是传递的值的形状不相等:

event_json_title = [title['title'] for title in event_json_read['events']['event']]
event_json_id = [name['id'] for cat in event_json_read['events']['event'] for name in cat['categories']['category']]
df = pd.DataFrame({'title':event_json_title},{'id':event_json_id})

我怀疑答案在于某些附加内容...我只是不确定如何将每个“ id”分组为“ title”。

2 个答案:

答案 0 :(得分:1)

假定响应变量是您调用的API的响应:

temp = list()    
for event in response['events']['event']:
  for category in event['categories']['category']:
    category['title'] = event['title'] 
    temp.append(category)
df = pd.DataFrame(temp).groupby('title').apply(lambda x: ", ".join(x['id']))

答案 1 :(得分:0)

这是一种方法。

例如:

event_json_title = [{"title": title['title'], "id": ",".join([i["id"] for i in title['categories']['category']])} for title in event_json_read['events']['event']]
df = pd.DataFrame.from_dict(event_json_title)
print(df["title"])
print "---"
print(df["id"])

输出:

0    Totally Random Book Club @ Lionel Bowen Library
1                        ABCs of Business Management
Name: title, dtype: object
---
0    books,attractions,community
1                 books,business
Name: id, dtype: object