这是我的资料
id var_map
0 7068 {'feature_1': 2.0, 'feature_2': 4.0, 'feature_3': 8.0, 'feature_4': 8.0}
1 7116 {'feature_1': '2', 'feature_2': 5.0, 'feature_3': 7.0}
2 7154 {'feature_1': 1.0, 'feature_2': 8.0, 'feature_3': 17.0}
这就是我想要的
id feature_1 feature_2 feature_3 feature_4 feature_5
0 7068 2.0 4.0 8.0 8.0
1 7116 2 5.0 7.0
2 7154 1.0 8.0 17.0
最佳答案
我相信需要pop
与DataFrame
构造函数和join
原始:
df = df.join(pd.DataFrame(df.pop('var_map').values.tolist(), index=df.index))
print (df)
id feature_1 feature_2 feature_3 feature_4
0 7068 2 4.0 8.0 8.0
1 7116 2 5.0 7.0 NaN
2 7154 1 8.0 17.0 NaN
但是,如果输入为
json
更好,则应使用json_normalize
。关于python - 如何将特征从JSON格式映射(弹出)到表格格式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50503246/