无法以字符串类型加入pandas数据框 [英] Unable to join pandas dataframe on string type
问题描述
我有两个DataFrames对象,其列如下所示
I have two DataFrames objects whose columns are as below
数据框1:
df.dtypes
输出:
ImageID object
Source object
LabelName object
Confidence int64
dtype: object
数据框2:
a.dtypes
输出:
LabelName object
ReadableName object
dtype: object
在这里,我正在尝试将这两个数据框合并如下
Here, i am trying to combine these two dataframes as below
combined = df.join(a,on='LabelName')
但是,我遇到以下错误
ValueError:您正在尝试合并object和int64列.如果 您希望继续进行操作,应该使用pd.concat
ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat
但是,我将它们合并到LabelName上,LabelName仅具有字符串(对象数据类型)
But, i am merging them on LabelName, which has only strings (object datatype)
我在这里想念东西吗?
推荐答案
关于 on
参数,文档显示:
About the on
parameter, the documentation says:
呼叫者中的列或索引级别名称要与其他索引一起连接,否则就按索引连接.
Column or index level name(s) in the caller to join on the index in other, otherwise joins index-on-index.
请注意,join()
始终使用other.index
.您可以尝试以下方法:
Note that join()
always uses other.index
. You can try this:
df.join(a.set_index('LabelName'), on='LabelName')
或改用df.merge()
.
这篇关于无法以字符串类型加入pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!