join two dataframes on common column

后端 未结 2 1538
轮回少年
轮回少年 2020-12-07 05:42

I want to join two data sources, orders and customers:

orders is an SQL Server table:

orderid| customerid | orderdate | ordercost
------ | ----------         


        
2条回答
  •  一个人的身影
    2020-12-07 06:07

    I think problem is columns customerid has different dtypes in both DataFrames so no match.

    So need convert both columns to int or both to str.

    df1['customerid'] = df1['customerid'].astype(int)
    df2['customerid'] = df2['customerid'].astype(int)
    

    Or:

    df1['customerid'] = df1['customerid'].astype(str)
    df2['customerid'] = df2['customerid'].astype(str)
    

    Also is possible omit how='inner', because default value of merge:

    merged= pd.merge( df1, df2, on= 'customerid')
    

提交回复
热议问题