【发布时间】:2020-03-10 00:04:37
【问题描述】:
我有一个数据框,其中有一列包含 JSON 之类的 -
Player ID Response
1 [{'id': '1-4', 'content': 'Develop'}, {'id': '1-3', 'content': 'Networking'}, {'id': '1-5', 'content': 'Opportunity'}]
2 [{'id': '1-4', 'content': 'Develop'}]
3 [{'id': '1-3', 'content': 'Networking'}, {'id': '1-4', 'content': 'Develop'}, {'id': '1-2', 'content': 'Excuse'}]
4 [{'id': '1-4', 'content': 'Develop'}, {'id': '1-6', 'content': 'Gain'}, {'id': '1-1', 'content': 'Different'}]
Response 列按顺序包含 1-3 个实体。我需要将此列重新排序为 -
ID Score InResponse
1-1 1 1
1-2 1 1
1-3 5 2
1-4 11 4
1-5 1 1
1-6 2 1
其中,如果一个 ID 排名第一,则获得 3 分,如果排名第二,则获得 2 分,如果排名第三,则获得 1 分。因此,例如,1-4 在 3 个响应中排名第 1,在 1 个响应中排名第 2,因此,3x3 + 1x2 = 11 pts。 InResponse 表示该 ID 在数据框中出现的次数。
我试过了
pd.io.json.json_normalize(df.Q1.to_dict())
但由于某种原因,它给了我意想不到的结果。我该怎么做?
【问题讨论】:
标签: json python-3.x pandas dataframe