【问题标题】:Python Merge Join with multiple join columns具有多个连接列的 Python 合并连接
【发布时间】:2020-09-28 13:12:58
【问题描述】:

我有两个结构如下的表:

    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 or citycountry = country"。我应该如何在 pandas 中做到这一点?

【问题讨论】:

    标签: python pandas dataframe merge


    【解决方案1】:

    将数据存储为DataFrames 后,您可以使用pandas 的merge 函数:

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

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多