熊猫数据帧:处理嵌套的不同对象,而不是字符串



我在 Pandas 中有一个数据帧列对象,如下所示:

{'totalSheets': 393, '_id': '59e52591265adce80, 'totalViews': 3640,}
{'totalSheets': 394, '_id': '59e6763a265adce80, 'totalViews': 3642,}

当我试图分裂成 str 时

df_split = df['column'].str.split(',',1)
print(df_split)

我有这个输出

0    NaN
1    NaN

所需的输出可以是

totalSheets             _id                       totalViews 
1       393                59e52591265adce80              3640
2       394                59e6763a265adce80              3642 

非常感谢您的帮助

pd.DataFrame(df['column'].tolist())
Out[354]: 
_id  totalSheets  totalViews
0  59e52591265adce80          393        3640
1  59e6763a265adce80          394        3642

我在你对Wen-Ben评论的回复中注意到这来自一个JSON。Pandas 有一个read_json函数,允许您直接从 JSON 文件创建数据框。根据您在列中的字符串,我会尝试pd.read_json('file.json', orient='index').根据文档:

东方:字符串

指示预期的 JSON 字符串格式。兼容的 JSON 字符串可以由具有相应定向值的 to_json() 生成。可能的方向集为:

'index' : 字典像 {索引 -> {列 -> 值}}