2015-01-12 28 views
-1

我試圖通過使用以下代碼重新標記/合併我的市場標籤。然而,一切都以超快的速度運行,並沒有實際的映射,但只是返回原始列。公司是表名,而company.market是市場的專欄。公司是一個數據框,熊貓和numpy是進口的。Python字典,Lambda x,map()不起作用

Eg. company_name market   value 
    'AA'   'Bio-Pharm'  $1,000 
    'BB'   'Biotechnology' $2,000 

我想用我的代碼重新標記公司AA的市場「Biothchnology」 ......

market_mapping = { 
'Bio-Pharm': 'Biotechnology', 
'Biomass Power Generation': 'Biotechnology', 
'Bioinformatics': 'Biotechnology', 
'Biometrics': 'Biotechnology', 
'Biotechnology and Semiconductor': 'Biotechnology', 
'Biofuels':'Biotechnology'} 

f = lambda x: market_mapping.get(x,x) 
company.market = company.market.map(f) 

你能幫我什麼這裏是錯的?預先感謝您的幫助:)

+0

'公司'和'company.market'是什麼? – BrenBarn

+0

'company.market'這是什麼變量? – GLHF

+0

嗨@BrenBarn我已經更新了我的問題。感謝您的評論! – ellie

回答

0

company是一個數據幀,market是一個對象,但不是一個字符串呢。 我修改了代碼:

f = lambda x: market_mapping.get(str(x),x) 

而且代碼正常工作!