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'>
任何幫助將是巨大的。謝謝。
您是否在尋找'pd.lookup' HTTPS://pandas.pydata.org/pandas-docs/stable /generated/pandas.DataFrame.lookup.html – Wen