熊猫数据框:使用嵌套的不同对象而不是字符串

时间:2019-03-27 17:44:26

标签: python pandas dataframe

我在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 

非常感谢您的帮助

2 个答案:

答案 0 :(得分:1)

检查

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

答案 1 :(得分:0)

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

  

orient:字符串

     
    

表示预期的JSON字符串格式。兼容的JSON字符串可以由to_json()生成,并带有相应的orient值。可能的东方方向是:

         
      

'index':类似{index->​​ {column-> value}}的字典