如何将 JSON 类型的字段解嵌套到一堆新字段中



所以我有一个数据帧,其中一列是 JSON 类型:

In [34]: df.iloc[0]
Out[34]:
user_id                                         lashdgfalsjdhgflajs
json_col               {'foo': True, 'bar': 666, 'baz': 'luhrmann'}
created                                  2019-01-16 07:02:30.137709
Name: 0, dtype: object

json_col 中的每条记录都有相同的架构 — 将其转换为更像的最佳方式是什么,在此过程中在每条记录中取消嵌套 JSON?

user_id                                         lashdgfalsjdhgflajs
foo                                                            True
bar                                                             666
baz                                                      'luhrmann'
created                                  2019-01-16 07:02:30.137709

显然,我可以用.apply()做一些事情,但我想知道我是否可以尝试更多熊猫式的东西。

使用

jscol=pd.DataFrame(df['json_col'].tolist(),index=df.index)    
yourdf=pd.concat([df.drop('json_col',1),jscol],axis=1)

最新更新