2017-09-14 114 views
2
sdf = sdf['Name1'].apply(lambda x: tryLookup(x, tdf)) 

tryLookup是一個函數,當前正在接受一個字符串,這是sdf列中的值Name1。我們使用apply將函數映射到sdf DataFrame中的每一行。熊貓從應用函數返回DataFrame?

而不是tryLookup返回一個字符串,有沒有辦法讓tryLookup返回一個我想合併的DataFrame與sdf DataFrame? tryLookup有一些額外的信息,我想將它們作爲新列添加到sdf中的所有行中。

所以對於tryLookup回報是這樣:

return pd.Series({'BEST MATCH': bestMatch, 'SIMILARITY SCORE': humanScore}) 

我試過的東西,如

sdf = sdf.merge(sdf['Name1'].apply(lambda x: tryLookup(x, tdf)), left_index=True, right_index=True) 

但這只是拋出

Traceback (most recent call last): 
    File "lookup.py", line 160, in <module> 
    main() 
    File "lookup.py", line 40, in main 
    sdf = sdf.merge(sdf['Name1'].apply(lambda x: tryLookup(x, tdf)), left_index=True, right_index=True) 
    File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 4618, in merge 
    copy=copy, indicator=indicator) 
    File "C:\Python27\lib\site-packages\pandas\tools\merge.py", line 58, in merge 
    copy=copy, indicator=indicator) 
    File "C:\Python27\lib\site-packages\pandas\tools\merge.py", line 473, in __init__ 
    'type {0}'.format(type(right))) 
ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'> 

任何幫助將是巨大的。謝謝。

+0

您是否在尋找'pd.lookup' HTTPS://pandas.pydata.org/pandas-docs/stable /generated/pandas.DataFrame.lookup.html – Wen

回答

0

嘗試用pandas.Series.to_frame轉換pd.Series到數據幀的記錄here

sdf = sdf.merge(sdf['Sold To Name (10)'].apply(lambda x: tryLookup(x, tdf)).to_frame(), left_index=True, right_index=True) 
+0

在這種情況下'sdf'是一個DataFrame。 – X33