基本上,我想这样做

pd.merge(Investments1,Investments2, how = "outer", left_on = left_key, right_on = right_key)


错误是

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


所以我检查right_on的类型:

Investments2['SECURITY CODE']
Name: SECURITY CODE, dtype: float64.


但是left_on发生了什么:

Investments2['security code']
Out[104]:
116     11111
118    222222
119    333333
Name: security code, dtype: object

type(Investments2.iloc[1,1])
Out[106]: float


是一种情况,一列浮点数是一个对象,而一个对象意味着一个字符串?

最佳答案

合并前先转换Investments2。喜欢,

Investments2['security code'] = Investments2['security code'].transform(float)


或在注释中指出,使用DataFrame.astype

Investments2['security code'] = Investments2['security code'].astype(float)

10-05 18:35