6
sdf = sdf['Name1'].apply(lambda x: tryLookup(x, tdf))

tryLookup is a function that is currently taking a string, which is the value of Name1 in the sdf column. We map the function using apply to every row in the sdf DataFrame.

Instead of tryLookup returning just a string, is there a way for tryLookup to return a DataFrame that I want to merge with the sdf DataFrame? tryLookup has some extra information, and I want to include that in the results by adding them as new columns to all the rows in sdf.

So the return for tryLookup is as such:

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

I tried something such as

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

But that just throws

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'>

Any help would be great. Thanks.

1

1 Answer 1

1

Try converting the pd.Series to a dataframe with pandas.Series.to_frame as documented here:

sdf = sdf.merge(sdf['Sold To Name (10)'].apply(lambda x: tryLookup(x, tdf)).to_frame(), left_index=True, right_index=True)
Sign up to request clarification or add additional context in comments.

1 Comment

sdf is a DataFrame in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.