【发布时间】:2020-04-03 03:40:57
【问题描述】:
我有一个数据框,其中有一列包含 JSON 的格式,其中一条记录看起来像 -
player_feedback
{'player': '1b87a117-09ef-41e2-8710-6bc144760a74',
'feedback': [{'answer': [{'id': '1-6gaincareerinfo', 'content': 'To gain career information'},
{'id': '1-5proveskills', 'content': 'Opportunity to prove skills by competing '},
{'id': '1-1diff', 'content': 'Try something different'}], 'question': 1},
{'answer': [{'id': '2-2skilldev', 'content': 'Skill development'}], 'question': 2},
{'answer': [{'id': '3-6exploit', 'content': 'Exploitation'},
{'id': '3-1forensics', 'content': 'Forensics'}], 'question': 3},
{'answer': 'verygood', 'question': 4},
{'answer': 'poor', 'question': 5}, ... ... ,
{'answer': 'verygood', 'question': 15}]}
这是data 的前 5 行。 我想将此列转换为单独的列,例如 -
player Question 1 Question 2 ... Question 15
1b87a117-09ef-41e2-8710-6bc144760a74 To gain career information, Skill development verygood
Opportunity to prove skills by competing,
Try something different
我从 -
df_survey_responses['player_feedback'].apply(ast.literal_eval).values.tolist()
但这只会让我在一个单独的字段中获得player id,并在另一个字段中获得反馈。据我所知,JSONNormalize 也会给我类似的结果。如何递归地执行此操作以获得我想要的结果,或者是更好的方法?
谢谢!
【问题讨论】:
标签: json python-3.x pandas dataframe