Pandas: merge left_on enter multiple columns of content

Created on 28 Nov 2018  路  2Comments  路  Source: pandas-dev/pandas

Code Sample, a copy-pastable example if possible

k=pd.DataFrame({
"a":"222222",
"b":"33333",
},index=[[0]])
m=pd.DataFrame({
"c":"222222",
"n":"6554413"
},index=[[0]])
pd.merge(k,m,left_on=("a","b"),right_on=("c","n"))
```

Problem description

I want to merge two dataframes using multiple connection keys,
But left_on can't pass in the list

help me

Expected Output

INSTALLED VERSIONS

commit: None
python: 2.7.14.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-327.el7.x86_64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.23.4
pytest: 3.3.2
pip: 9.0.1
setuptools: 40.2.0
Cython: 0.27.3
numpy: 1.12.1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.4.1
sphinx: 1.6.6
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: 0.9.2
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Usage Question

Most helpful comment

This works ok for me on master. You just don't have any matches in your key columns.

Try this example:

k=pd.DataFrame({ 
    "a":["222222", "123"], 
    "b":["33333", "abc"] 
    },index=[[0,1]]
) 
m=pd.DataFrame({ 
    "c":["222222", "123"], 
    "n":["6554413", "abc"] 
    },index=[[0,1]]
)
pd.merge(k,m,left_on=("a","b"),right_on=("c","n"))

returns:

     a    b    c    n
0  123  abc  123  abc

All 2 comments

This works ok for me on master. You just don't have any matches in your key columns.

Try this example:

k=pd.DataFrame({ 
    "a":["222222", "123"], 
    "b":["33333", "abc"] 
    },index=[[0,1]]
) 
m=pd.DataFrame({ 
    "c":["222222", "123"], 
    "n":["6554413", "abc"] 
    },index=[[0,1]]
)
pd.merge(k,m,left_on=("a","b"),right_on=("c","n"))

returns:

     a    b    c    n
0  123  abc  123  abc

thank you

Was this page helpful?
0 / 5 - 0 ratings