ValueError: You are trying to merge on object and int64 columns

博客讨论了在使用Pandas进行数据合并时遇到的ValueError,原因是尝试在对象和整数列之间进行合并。文章提供了两种场景下出现此错误的可能性,并给出了修正错误的代码示例,强调了在进行join操作时应确保列类型一致,可通过设置索引来正确合并DataFrame。
摘要由CSDN通过智能技术生成

错误如下:

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

原代码:

pca = pd.read_csv("I:/1000GenomeProject/result/plink.eigenvec"," ",header=None)
ped = pd.read_csv("I:/1000GenomeProject/result/20130606_g1k.ped","\\t",engine='python')
#重命名pca文件的列
pca = pca.rename(columns=dict([(1,"Individual ID")]+[(x,"PC"+str(x-1)) for x in range(2,22)]))

#join两个数据表
#报错写法
pcaped=pca.join(ped,on="Individual ID",how="inner")

#正确写法
pcaped = pca.set_index('Individual ID').join(ped.set_index('Individual ID'),how="inner")

解决方案:

It can occur in two scenarios:

When using the join method: you are probably joining DataFrames on labels and not on indices.
When using the merge method: you are probably joining DataFrames on two columns that are not of the same type.

You are trying to join on labels and not on indices using the join method
This is an example that generates the error:

data_x.join(data_y, on='key')

In the first scenario, you can edit your code to join on the index. In the following code, I set the index on the columns I want to join.

data_x.set_index('key').join(data_y.set_index('key'))

参考:
https://www.roelpeters.be/pandas-solve-you-are-trying-to-merge-on-object-and-int64-columns/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值