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)

以上,问题解决~

相关文章
|
3月前
|
SQL 数据采集 数据挖掘
Pandas数据合并:concat与merge
Pandas是Python中强大的数据分析库,提供灵活高效的数据结构和工具。本文详细介绍了Pandas中的两种主要合并方法——`concat`和`merge`。`concat`用于沿特定轴连接多个Pandas对象,适用于简单拼接场景;`merge`则类似于SQL的JOIN操作,根据键合并DataFrame,支持多种复杂关联。文章还探讨了常见问题及解决方案,如索引对齐、列名冲突和数据类型不一致等,帮助读者全面掌握这两种方法,提高数据分析效率。
83 8
|
10月前
|
数据处理 索引 Python
使用pandas的merge()和join()函数进行数据处理
使用pandas的merge()和join()函数进行数据处理
172 2
|
5月前
|
存储 数据采集 数据处理
Pandas中批量转换object至float的高效方法
在数据分析中,常需将Pandas DataFrame中的object类型列转换为float类型以进行数值计算。本文介绍如何使用`pd.to_numeric`函数高效转换,并处理非数字值,包括用0或平均值填充NaN值的方法。
269 1
|
6月前
|
SQL 数据采集 索引
聚焦Pandas数据合并:掌握merge方法
聚焦Pandas数据合并:掌握merge方法
67 0
|
8月前
|
并行计算 Serverless API
函数计算操作报错合集之出现 "AttributeError: 'NoneType' object has no attribute 'pop'" 错误,是什么原因
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
244 1
|
7月前
|
Ubuntu
Ubuntu22.04,AOSP编译报错: libncurses.so.5: cannot open shared object file: No such file
本文描述了在Ubuntu 22.04系统上编译AOSP时遇到的`libncurses.so.5`缺失错误,并提供了通过安装相应库解决该问题的步骤。
1002 0
|
10月前
|
JSON 数据格式
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
|
10月前
|
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
422 0
|
8月前
|
Python
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
684 0
|
8月前
|
开发者 Python
【Python】已解决:(pandas read_excel 读取Excel报错)ImportError: Pandas requires version ‘2.0.1’ or newer of ‘x
【Python】已解决:(pandas read_excel 读取Excel报错)ImportError: Pandas requires version ‘2.0.1’ or newer of ‘x
448 0