我已经输入了结构:
[{"id": 1, "name": "John", "events": [{"date": "2018-10-10", "value": 1}]},
{"id": 2, "name": "Kyle", "events": [{"date": "2018-11-10", "value": 2}]}]
加载到DataFrame
后,事件存储为Series
。
我需要将事件系列转换为这样的列:
id name 2018-10-10 2018-11-10
1 John 1 0
2 Kyle 0 2
如何扁平化熊猫的这种结构?
答案 0 :(得分:4)
将json_normalize
与set_index
和unstack
一起使用:
L = [{"id": 1, "name": "John", "events": [{"date": "2018-10-10", "value": 1}]},
{"id": 2, "name": "Kyle", "events": [{"date": "2018-11-10", "value": 2}]}]
from pandas.io.json import json_normalize
df = json_normalize(L, 'events', ['id','name'])
df = (df.set_index(['id','name','date'])['value']
.unstack(fill_value=0)
.reset_index()
.rename_axis(None, axis=1))
print (df)
id name 2018-10-10 2018-11-10
0 1 John 1 0
1 2 Kyle 0 2