我有以下格式的数据框。我正在尝试将每个键值对分为不同的行。
id, data
101, [{'field': 'type1', 'newValue': '2020-01-16T12:35:50Z', 'oldValue': None},
{'field': 'status', 'newValue': 'Started', 'oldValue': None}]
预期产量:
id, field, newValue, oldValue
101, type1, 2020-01-16T12:35:50Z, None
101, status, Started, None
你可以这样做:
In [4432]: df = pd.DataFrame({'id':[101], 'data':[[{'field': 'type1', 'newValue': '2020-01-16T12:35:50Z', 'oldValue': None}, {'field': 'status', 'newValue': 'Started', 'oldValue': None}]]})
In [4438]: df1 = df.explode('data')['data'].apply(pd.Series)
In [4440]: df = pd.concat([df.id, df1], axis=1)
In [4441]: df
Out[4441]:
id field newValue oldValue
0 101 type1 2020-01-16T12:35:50Z None
0 101 status Started None
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句