简体   繁体   English

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

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

When I try to join two dataframes, I keep getting the following problem. 当我尝试连接两个数据框时,我一直遇到以下问题。 They are of two different types (float64 and object), but the column I am trying to join on should be the same data type for each of the two dataframes. 它们是两种不同的类型(float64和object),但是对于两个数据帧中的每一个,我尝试加入的列都应具有相同的数据类型。

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

When I try to join: 当我尝试加入时:

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

The products_df.PROD_NBR lists all product numbers for the entire company. products_df.PROD_NBR列出整个公司的所有产品编号。 The postransaction_df.PROD_NBR are in regards to the item sold at the time. postransaction_df.PROD_NBR与当时出售的商品有关。 Shouldn't I be able to join these? 我不应该可以加入这些吗?

I am completely stuck. 我完全被困住了。 Any help would be appreciated. 任何帮助,将不胜感激。

It happens when common column in both table are of different data type 当两个表中的公共列具有不同的数据类型时,就会发生这种情况
you have to convert one of the datatype by using df.PROD_NBR.astype(int) or df.PROD_NBR.astype(float) 您必须使用df.PROD_NBR.astype(int)df.PROD_NBR.astype(float)转换数据类型之一

then 然后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM