Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.

简介: Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.

Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.


1、需求:

df1  和 df2 按照 A, B 两列进行合并,假设 df1 为 A B C 三列,df2 为 A B D 三列,将其中A B 相同的列 merge 为 A B C D 四列。

outfile = pd.merge(df1, df2, how='left', on=['A','B'])

2、用 pandas 合并两个 CSV 文件时,报错信息如下:

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

3、尝试解决:

虽然建议我用 pd.concat() ,但显然 concat() 函数只是简单拼接,并不符合我希望把两个表内 A B 两列值相同的行合并在一起的需求。

使用 dtype() 函数输出想merge 的A B 列的类型:

既然 A 列是 object 类型, B 列是 int64 类型,所以将 B 列转为 int 类型:

df['A'] = df['A'].apply(pd.to_numeric)

以上,问题解决~

相关文章
|
2月前
|
数据处理 索引 Python
使用pandas的merge()和join()函数进行数据处理
使用pandas的merge()和join()函数进行数据处理
50 2
|
3天前
|
消息中间件 监控 Serverless
函数计算操作报错合集之显示报错:RecursionError: maximum recursion depth exceeded while calling a Python object,该如何解决
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
|
2月前
|
API Android开发
android setTag (int key, Object tag)使用
android setTag (int key, Object tag)使用
16 1
|
2月前
|
JavaScript
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
140 0
|
2月前
|
JSON 数据格式
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
|
2月前
R语言ggsurvplot绘制生存曲线报错 : object of type ‘symbol‘ is not subsettab
R语言ggsurvplot绘制生存曲线报错 : object of type ‘symbol‘ is not subsettab
|
9月前
|
编解码 Python
pandas - read_csv报错:‘utf-8‘/‘gbk‘ codec can‘t decode byte 0xb1 in position 0:invalid start byte
pandas - read_csv报错:‘utf-8‘/‘gbk‘ codec can‘t decode byte 0xb1 in position 0:invalid start byte
173 0
|
9月前
|
Python Windows
xlrd库报错【AttributeError: ‘ElementTree‘ object has no attribute ‘getiterator‘】
xlrd库报错【AttributeError: ‘ElementTree‘ object has no attribute ‘getiterator‘】
151 0
|
7月前
报错modify sync object Modify sync object Failed!
报错modify sync object Modify sync object Failed!
27 1
|
11月前
|
PyTorch 算法框架/工具
已解决虚拟机yolov5报错:AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
已解决虚拟机yolov5报错:AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
299 0
已解决虚拟机yolov5报错:AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘