我有一个数据框,看起来如下
df1 = df = pd.DataFrame({"a":[[{"x":0,"y":4}], None, [{"x":10,"y":5}], None],
"b":[5, 6, 7, 8]})
如何从中创建x和y的单独列?
答案 0 :(得分:3)
您可以这样做:
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