如何将特定的dtype对象列的字段转换为pandas中的数据帧列

时间:2016-01-25 11:37:47

标签: python mongodb pandas dataframe

我尝试将对象类型列page_view_count字段转换为数据框列。

我有一个数据框:

    _id                         page_view_count
    568a8c25cac4991645c287ac    {u'main-rating': 2, u'detailed-rating2': 1, u'detailed-rating': 2}      
    568cd22e9e82dfc166d7dff1    {u'main-rating': 1, u'thank-you': 1, u'detailed-rating2': 1, u'detailed-rating': 1, u'comments': 1}     
    568e5a38b4a797c664143dda    {u'main-rating': 1, u'detailed-rating2': 1, u'detailed-rating': 1}  
    568e5a561ae56e09656bfb99    {u'main-rating': 1, u'detailed-rating': 1}
    56b24c651fd6901e0ac262e4    nan 
    568df45a177e30c6487d3600    {u'main-rating': 1, u'thank-you': 1, u'detailed-rating2': 1, u'detailed-rating': 1, u'comments': 1}

我想将page_view_count的字段作为数据框的列:

_id                         main-rating     detailed-rating     detailed-rating2    comments    thank-you
568a8c25cac4991645c287ac    2               1                   1                   nan         nan
568cd22e9e82dfc166d7dff1    1               1                   1                   1           1
568e5a38b4a797c664143dda    1               1                   1                   nan         nan
568e5a561ae56e09656bfb99    1               1                   nan                 nan         nan
56b24c651fd6901e0ac262e4    nan             nan                 nan                 nan         nan
568df45a177e30c6487d3600    1               1                   1                   1           1

有什么办法吗?

1 个答案:

答案 0 :(得分:1)

您可以从列page_view_countjoin_id创建新的数据框。最后sort_index

df1 = pd.DataFrame([x for x in df['page_view_count']]).join(df['_id'])
df1 = df1.sort_index(1)
print df1
                            _id  comments  detailed-rating  detailed-rating2  \
0      568a8c25cac4991645c287ac       NaN                2                 1   
1      568cd22e9e82dfc166d7dff1         1                1                 1   
2      568e5a38b4a797c664143dda       NaN                1                 1   
3      568e5a561ae56e09656bfb99       NaN                1               NaN   
4      568df45a177e30c6487d3600         1                1                 1   

   main-rating  thank-you  
0            2        NaN  
1            1          1  
2            1        NaN  
3            1        NaN  
4            1          1  

编辑:

NaN加入时仍然存在问题。 解决方案是将NaN替换为fillna以清空dictionary,然后创建Dataframe

import pandas as pd
import numpy as np


df = pd.DataFrame([[1,  {'name':'Jack','email':'abc'} ], 
                     [2, np.nan],
                     [3,  {'name':'Ram','email':'xyz'}  ],
                     ], columns=['_id','page_view_count'])

print df[df['page_view_count'].isnull()].index
#Int64Index([1], dtype='int64')

print pd.Series([{}], index=df[df['page_view_count'].isnull()].index , name='page_view_count')
#1    {}
#Name: page_view_count, dtype: object

df['page_view_count'] = df['page_view_count'].fillna(pd.Series([{}], index=df[df['page_view_count'].isnull()].index , name='page_view_count'))
print df
#   _id                       page_view_count
#0    1  {u'name': u'Jack', u'email': u'abc'}
#1    2                                    {}
#2    3   {u'name': u'Ram', u'email': u'xyz'}


df1 = pd.DataFrame([x for x in df['page_view_count']]).join(df['_id'], how='right')
df1 = df1.sort_index(1)
print df1
#   _id email  name
#0    1   abc  Jack
#1    2   NaN   NaN
#2    3   xyz   Ram