Reputation: 99
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.
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. The postransaction_df.PROD_NBR are in regards to the item sold at the time. Shouldn't I be able to join these?
I am completely stuck. Any help would be appreciated.
Upvotes: 0
Views: 65
Reputation: 895
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)
then
products_df.merge(postransaction_df, products_df, on='PROD_NBR')
Upvotes: 1