Skip to main content
 首页 » 编程设计

python之无法加入字符串类型的 Pandas 数据框

2025年05月04日27xiaohuochai

我有两个 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: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat

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

我是不是漏掉了什么?

请您参考如下方法:

关于 on parameter ,文档说:

Column or index level name(s) in the caller to join on the index in other, otherwise joins index-on-index.

请注意,join() 始终使用 other.index。你可以试试这个:

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

或者使用 df.merge() 代替。