我有一个以下格式的数据框。我试图将每个键值对分成不同的行。
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
最佳答案
让我们在data
上分解
数据框,然后从分解的data
列创建一个新的数据框,最后使用join
:
out = df.explode('data').reset_index(drop=True)
out = out.join(pd.DataFrame(out.pop('data').tolist()))
print(out)
id field newValue oldValue
0 101 type1 2020-01-16T12:35:50Z None
1 101 status Started None
关于python - Pandas - 从列中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65130798/