为什么我不能将这两个数据框连接在一起?

Why can't I join these two data frames together?

当我尝试连接两个数据帧时,我不断遇到以下问题。它们有两种不同的类型(float64 和对象),但我尝试加入的列对于两个数据帧中的每一个都应该是相同的数据类型。

products_df.PROD_NBR
Out[13]: 
0        -7.358825e+10
1        -7.358821e+10
2        -7.204736e+10
3        -7.204735e+10
4        -7.204735e+10
              ...     
189047    9.940000e+22
189048    9.940000e+22
189049    9.950000e+22
189050    9.950000e+22
189051    9.950000e+22
Name: PROD_NBR, Length: 189052, dtype: float64

postransaction_df.PROD_NBR
Out[14]: 
0          1164203101
1         72047351000
2          3600025824
3          7205861079
4         82775501058
             ...     
915739     3660081331
915740    34580265065
915741    31101710042
915742     3927832300
915743    74098527503
Name: PROD_NBR, Length: 915744, dtype: object

当我尝试加入时:

pd.merge(postransaction_df, products_df, on='PROD_NBR')
...
ValueError: You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat

products_df.PROD_NBR 列出了整个公司的所有产品编号。 postransaction_df.PROD_NBR 与当时售出的商品有关。我不应该加入这些吗?

我完全卡住了。任何帮助将不胜感激。

当 table 中的公共列具有不同的数据类型时会发生这种情况
您必须使用以下方法转换其中一种数据类型 df.PROD_NBR.astype(int)df.PROD_NBR.astype(float)

然后

products_df.merge(postransaction_df, products_df, on='PROD_NBR')