我正在以格式
从json文件中提取数据集{"Y": 5, "X": [626,1,2,3]}, {"Y": 3, "X": [3]}, {"Y": 5, "X": [41847,1233]}
我将它从json文件中解压缩到pandas数据帧:
X Y
[626,1,2,3] 5
[3] 3
[41847,1233] 5
如何将其转换为
X1 X2 X3 X4 Y
626 1 2 3 5
3 NAN NAN NAN 3
41847 1233 NAN NAN 5
答案 0 :(得分:0)
您可以使用apply
循环浏览X
列中的项目,并将每个列表转换为Series
对象,然后会为您提供包含列表中元素的数据框作为列:
pd.concat([df.X.apply(pd.Series).rename(columns="X{}".format), df.Y], axis=1)
# X0 X1 X2 X3 Y
#0 626.0 1.0 2.0 3.0 5
#1 3.0 NaN NaN NaN 3
#2 41847.0 1233.0 NaN NaN 5
df.X.apply(pd.Series)
# 0 1 2 3
#0 626.0 1.0 2.0 3.0
#1 3.0 NaN NaN NaN
#2 41847.0 1233.0 NaN NaN