Pandas merge using dfA column == dfB index [duplicate]

落花浮王杯 提交于 2020-01-11 11:56:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!