【发布时间】:2018-02-23 18:05:08
【问题描述】:
sdf = sdf['Name1'].apply(lambda x: tryLookup(x, tdf))
tryLookup 是一个函数,它当前正在接受一个字符串,它是 sdf 列中Name1 的值。我们使用 apply 将函数映射到sdf DataFrame 中的每一行。
tryLookup 不是只返回一个字符串,有没有办法让tryLookup 返回一个我想与sdf 数据帧合并的数据帧? 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?pandas.pydata.org/pandas-docs/stable/generated/…
标签: python python-2.7 pandas