2016-11-25 90 views
-1

我有一本字典,看起來像這樣:創建基於字典的新熊貓列的值

{'Andrew':['Photography'],'Peter':['Travel']} 

我也有一個分貝,看起來像這樣:到

ID  Hobby 
1  Travel 
2  Photography 
3  Football 
.......... 

我想添加thirth列數據框來實現:

ID  Hobby  Name 
1  Travel  Peter 
2  Photography Andrew 
3  Football  NaN 

我也覺得這Adding a new pandas column with mapped value from a dictionary,但我不需要合作lumn包含字典中的值,但是包含關鍵字。 感謝

+1

那麼是什麼阻止你與建設的密鑰和值的新字典逆轉嗎?之後,鏈接的問題將爲你工作? – EdChum

+0

我現在感覺很傻。這確實奏效 –

回答

0

你可以嘗試這樣的事情:

import pandas as pd 
equiv = {'Andrew':['Photography'],'Peter':['Travel'], 'Jack': ['Football']} 

df = pd.DataFrame({'Hobby':[equiv[x][0] for x in equiv], 'Name': [key for key in equiv.keys()]}) 
print(df) 
相關問題