Unable to join pandas dataframe on string type
问题内容:
I have two DataFrames objects whose columns are as below
Dataframe 1:
df.dtypes
Output:
ImageID object
Source object
LabelName object
Confidence int64
dtype: object
Dataframe 2:
a.dtypes
Output:
LabelName object
ReadableName object
dtype: object
Here, i am trying to combine these two dataframes as below
combined = df.join(a,on='LabelName')
But, i am getting the following error
ValueError: You are trying to merge on object and int64 columns. If you wish
to proceed you should use pd.concat
But, i am merging them on LabelName, which has only strings (object datatype)
Am i missing something here?
问题答案:
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.
Note that join()
always uses other.index
. You can try this:
df.join(a.set_index('LabelName'), on='LabelName')
Or use df.merge()
instead.