I am trying to do a left outer join
on two Data.Frames using Python. The goal is to get a column from right into left based on if the key from left exists in the list from right.
My initial thought was to use Pandas, so I wrote something like this:
import pandas as pd
left = pd.DataFrame({'name':['spam', 'ham', 'eggs'], 'leftkey':[11, 22, 33]})
right = pd.DataFrame({'var':['foo', 'bar'], 'rightkey':[[1, 2, 5], [2, 33, 100]]})
merged = pandas.merge(left, right, left_on='keyleft', right_on='keyright', how='left')
As we can see, left_on
is a single variable, while right_on
is a list
.
I would expect merged
to look something like this:
| | name | leftkey | var | rightkey |
|---|------|---------|-----|------------|
| 0 | spam | 11 | NaN | NaN |
| 1 | ham | 22 | NaN | NaN |
| 2 | eggs | 33 | bar | [2,33,100] |
However, all of var
and rightkey
end up being NaN
.
I realize that I could just put everything in R and have this done. Perhaps I'm overthinking things and this does not even require Pandas. However, my hope is to keep the pipeline in Python for as long as possible.
Any suggestions?