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)

以上,问题解决~

相关文章
|
1天前
|
存储 数据采集 数据处理
Pandas中批量转换object至float的高效方法
在数据分析中,常需将Pandas DataFrame中的object类型列转换为float类型以进行数值计算。本文介绍如何使用`pd.to_numeric`函数高效转换,并处理非数字值,包括用0或平均值填充NaN值的方法。
|
5月前
|
数据处理 索引 Python
使用pandas的merge()和join()函数进行数据处理
使用pandas的merge()和join()函数进行数据处理
99 2
|
20天前
|
SQL 数据采集 索引
聚焦Pandas数据合并:掌握merge方法
聚焦Pandas数据合并:掌握merge方法
25 0
|
3月前
|
Python
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
156 0
|
3月前
|
开发者 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
160 0
|
3月前
|
API 索引 Python
【Pandas】已完美解决:AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘
【Pandas】已完美解决:AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘
131 0
|
5月前
|
API Android开发
android setTag (int key, Object tag)使用
android setTag (int key, Object tag)使用
42 1
|
12月前
|
编解码 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
297 0
|
索引 Python
Pandas 的Merge函数详解
在日常工作中,我们可能会从多个数据集中获取数据,并且希望合并两个或多个不同的数据集。这时就可以使用Pandas包中的Merge函数。在本文中,我们将介绍用于合并数据的三个函数
168 1
|
Cloud Native Go Python
解决Pandas KeyError: “None of [Index([...])] are in the [columns]“问题
解决Pandas KeyError: “None of [Index([...])] are in the [columns]“问题
330 0