无法以字符串类型加入pandas数据框


问题内容

我有两个DataFrames对象,其列如下

数据框1:

df.dtypes

输出:

ImageID       object
Source        object
LabelName     object
Confidence     int64
dtype: object

数据框2:

a.dtypes

输出:

LabelName       object
ReadableName    object
dtype: object

在这里,我试图将这两个数据框组合如下

combined =  df.join(a,on='LabelName')

但是,我收到以下错误

ValueError:您正在尝试合并object和int64列。如果要继续,则应使用pd.concat

但是,我将它们合并到只有字符串(对象数据类型)的LabelName上

我在这里想念什么吗?


问题答案:

关于on参数,文档说:

调用者中的列或索引级别名称要与其他索引一起联接,否则就联接按索引进行索引。

请注意,join()始终使用other.index。您可以尝试以下方法:

df.join(a.set_index('LabelName'), on='LabelName')

df.merge()改为使用。