从词典字典创建pandas数据帧

时间:2015-10-15 20:04:16

标签: dictionary pandas dataframe

我有一个字典词典:

{'user':{movie:rating} }

例如,

{Jill': {'Avenger: Age of Ultron': 7.0,
                            'Django Unchained': 6.5,
                            'Gone Girl': 9.0,
                            'Kill the Messenger': 8.0}
'Toby': {'Avenger: Age of Ultron': 8.5,
                                'Django Unchained': 9.0,
                                'Zoolander': 2.0}}

我想将这个dicts的dict转换为pandas数据帧,第1列是用户名,其他列是电影评级,即

user  Gone_Girl  Horrible_Bosses_2  Django_Unchained  Zoolander etc. \

但是,有些用户没有为电影评分,因此这些电影不包含在该用户键()的值()中。在这些情况下,用NaN填充条目会很好。

截至目前,我遍历密钥,填写列表,然后使用此列表创建数据框:

data=[] 
for i,key in enumerate(movie_user_preferences.keys() ):
    try:            
        data.append((key
                    ,movie_user_preferences[key]['Gone Girl']
                    ,movie_user_preferences[key]['Horrible Bosses 2']
                    ,movie_user_preferences[key]['Django Unchained']
                    ,movie_user_preferences[key]['Zoolander']
                    ,movie_user_preferences[key]['Avenger: Age of Ultron']
                    ,movie_user_preferences[key]['Kill the Messenger']))
    # if no entry, skip
    except:
        pass 
df=pd.DataFrame(data=data,columns=['user','Gone_Girl','Horrible_Bosses_2','Django_Unchained','Zoolander','Avenger_Age_of_Ultron','Kill_the_Messenger'])

但这只给了我一个用户评估集合中所有电影的数据框。

我的目标是通过迭代电影标签(而不是上面显示的暴力方法)附加到数据列表,其次,创建一个包含所有用户的数据框,并将空值放在不包含所有用户的元素中有电影评级。

2 个答案:

答案 0 :(得分:40)

您可以将dict的dict传递给DataFrame构造函数:

In [11]: d = {'Jill': {'Django Unchained': 6.5, 'Gone Girl': 9.0, 'Kill the Messenger': 8.0, 'Avenger: Age of Ultron': 7.0}, 'Toby': {'Django Unchained': 9.0, 'Zoolander': 2.0, 'Avenger: Age of Ultron': 8.5}}

In [12]: pd.DataFrame(d)
Out[12]:
                        Jill  Toby
Avenger: Age of Ultron   7.0   8.5
Django Unchained         6.5   9.0
Gone Girl                9.0   NaN
Kill the Messenger       8.0   NaN
Zoolander                NaN   2.0

或使用from_dict方法:

In [13]: pd.DataFrame.from_dict(d)
Out[13]:
                        Jill  Toby
Avenger: Age of Ultron   7.0   8.5
Django Unchained         6.5   9.0
Gone Girl                9.0   NaN
Kill the Messenger       8.0   NaN
Zoolander                NaN   2.0

In [14]: pd.DataFrame.from_dict(d, orient='index')
Out[14]:
      Django Unchained  Gone Girl  Kill the Messenger  Avenger: Age of Ultron  Zoolander
Jill               6.5          9                   8                     7.0        NaN
Toby               9.0        NaN                 NaN                     8.5          2

答案 1 :(得分:0)

这种蛮力方法似乎也有效,但在我看来,迭代电影标签仍然会更加强大。

data=[] 
for i,key in enumerate(movie_user_preferences.keys() ):
    try:            
        data.append((key
                    ,movie_user_preferences[key]['Gone Girl'] if 'Gone Girl' in movie_user_preferences[key] else 'NaN'
                    ,movie_user_preferences[key]['Horrible Bosses 2'] if 'Horrible Bosses 2' in movie_user_preferences[key] else 'NaN'
                    ,movie_user_preferences[key]['Django Unchained'] if 'Django Unchained' in movie_user_preferences[key] else 'NaN'
                    ,movie_user_preferences[key]['Zoolander'] if 'Zoolander' in movie_user_preferences[key] else 'NaN'
                    ,movie_user_preferences[key]['Avenger: Age of Ultron'] if 'Avenger: Age of Ultron' in movie_user_preferences[key] else 'NaN'
                    ,movie_user_preferences[key]['Kill the Messenger'] if 'Kill the Messenger' in movie_user_preferences[key] else 'NaN' ))

    # if no entry, skip
    except:
        pass


 user Gone_Girl Horrible_Bosses_2  Django_Unchained Zoolander  \
 0      Sam         6                 3               7.5         7   
 1      Max        10                 6               7.0        10   
 2   Robert       NaN                 5               7.0         9   
 3     Toby       NaN               NaN               9.0         2   
 4    Julia       6.5               NaN               6.0       6.5   
 5  William         7                 4               8.0         4   
 6     Jill         9               NaN               6.5       NaN   

 Avenger_Age_of_Ultron Kill_the_Messenger  
 0                   10.0                5.5  
 1                    7.0                  5  
 2                    8.0                  9  
 3                    8.5                NaN  
 4                   10.0                  6  
 5                    6.0                6.5  
 6                    7.0                  8