Cannot join two dataframes in pandas. ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat

Viewed 160

I have two dataframes I want to join

df1.join(df2, how = 'left', on = "foo")

I get

ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat

But, both columns foo are of type object.

print(df1.dtypes)

This gives me

foo         object
geometry    geometry
dtype: object

Looking at the dtypes of df2

print(df2.dtypes)

This gives me

foo         object
bar         object
num         int64
dtype: object

foo is in both dataframe of type object. Even a

df1.dtypes["foo"] == df2.dtypes["foo"]

returns a True

Why do I get this error message?

1 Answers

df1.join(df2, on='foo') attempts to join df1['foo'] to the index of df2 (which is likely int64). To merge two dataframes on columns use pd.merge

Related