问题
How to merge (left join) using column value
from dataframe A and index of dataframe B?
For example:
>>> A >>> B
lkey value rkey value
0 foo 1 0 foo 5
1 bar 2 1 bar 6
2 baz 3 2 qux 7
3 foo 4 3 bar 8
to get:
lkey value_x rkey value_y
0 foo 1 bar 6
1 bar 2 qux 7
2 baz 3 bar 8
3 foo 4 NaN NaN
回答1:
try using left_on
and right_index
to do the merging, like:
m = pd.merge(dfA, dfB, right_index = True, left_on='value')
来源:https://stackoverflow.com/questions/42802023/pandas-merge-using-dfa-column-dfb-index