python - Pandas - 从列中提取值

标签 python pandas dataframe

我有一个以下格式的数据框。我试图将每个键值对分成不同的行。

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/

相关文章:

python - Django中同一模型的多个多对多关系

python - PyQt 对话框 - 如何让它在按下按钮后退出?

python - 未知的字符问题�

从数据框列表的最后一列中删除字符

python - 在 Python 中复制和写入 XML 节点

python - 在 Komodo ide 中使用 Pyral 包

python-3.x - 我可以在 Pandas 数据框上应用 Groupby 并计算所有列的平均值吗?

python - 我必须将 Pandas DataFrame 每一行的数据与其余行的数据进行比较,有没有办法加快计算速度?

Python Pandas Merge --- 哪些行没有合并?

python - 在 Pandas 中组合日期数据框和值数据框