具有多个联接列的Python合并联接

具有多个联接列的Python合并联接,python,pandas,dataframe,merge,Python,Pandas,Dataframe,Merge,我有两张表,结构如下: Table 1 ID City Country 1 India 2 Delhi 3 America 4 New York 5 Germany Table 2 ID Country City 1 India 2 India Delhi 3 America 4 America New York 5 Germany Select * from table1

我有两张表,结构如下:

    Table 1 
ID  City Country    
1   India   
2   Delhi   
3   America 
4   New York    
5   Germany 

Table 2     
ID  Country City
1   India   
2   India   Delhi
3   America 
4   America New York
5   Germany 


Select  * from table1           
Left outer join table2          
on citycountry  = city or citycountry = country         

我的任务是针对多个加入条件在pandas中实现相同的功能
“citycountry=city或citycountry=country”
。我应该如何在pandas中执行此操作?

一旦您将数据存储为,就可以使用pandas的功能:

import pandas as pd
pd.merge(table1, table2, 
     left_on=  ['citycountry', 'citycountry'],
     right_on= ['country', 'city'], 
     how = 'left')