将dict列表转换为pandas中的行[重复]

Posted

技术标签:

【中文标题】将dict列表转换为pandas中的行[重复]【英文标题】:Converting list of dict to rows in pandas [duplicate] 【发布时间】:2020-12-07 17:10:51 【问题描述】:

我有一个数据框如下所示

df1 = df = pd.DataFrame("a":[["x":0,"y":4], None, ["x":10,"y":5], None], 
                     "b":[5, 6, 7, 8]) 

如何从中创建一个单独的 x 和 y 列?

【问题讨论】:

【参考方案1】:

你可以这样做:

df[['x', 'y']] = df['a'].str[0].apply(pd.Series)

print(df)

                     a  b     x    y
0   ['x': 0, 'y': 4]  5   0.0  4.0
1                 None  6   NaN  NaN
2  ['x': 10, 'y': 5]  7  10.0  5.0
3                 None  8   NaN  NaN

【讨论】:

以上是关于将dict列表转换为pandas中的行[重复]的主要内容,如果未能解决你的问题,请参考以下文章