当前位置: 首页 > 知识库问答 >
问题:

df.join()的问题:valueError:您试图合并object和int64列

林承悦
2023-03-14

这些问题都没有涉及这个问题:问题1和问题2,我在pandas文档中也找不到答案。

您好,我正在尝试查找此错误的潜在原因:

ValueError: You are trying to merge on object and int64 columns.
  account  apt  apt_p  balance       date  day    flag  month  reps     reqid  year
0  AA0420    0    0.0  -578.30 2019-03-01    1       1      3    10  82f2d761  2019
1  AA0420    0    0.1  -578.30 2019-03-02    2       1      3    10  82f2d761  2019
2  AA0420    0    0.1  -578.30 2019-03-03    3       1      3    10  82f2d761  2019
3  AA0421    0    0.1  -607.30 2019-03-04    4       1      3    10  82f2d761  2019
4  AA0421    0    0.1  -610.21 2019-03-05    5       1      3    10  82f2d761  2019

打印(the_big_df.info())输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 36054 entries, 0 to 36053
Data columns (total 11 columns):
account        36054 non-null object
apt            36054 non-null int64
apt_p          36054 non-null float64
balance        36054 non-null float64
date           36054 non-null datetime64[ns]
day            36054 non-null int64
flag           36054 non-null int64
month          36054 non-null int64
reps           36054 non-null int32
reqid          36054 non-null object
year           36054 non-null int64
dtypes: datetime64[ns](1), float64(2), int32(1), int64(5), object(2)
memory usage: 3.2+ MB

下面是我传递给join()的数据文件打印(df_to_join.head(5)):

      reqid     id
0  54580f39  13301
1  3ba905c0  77114
2  5f2d80da  13302
3  a1478e98  77115
4  9b09854b  78598

打印(df_to_join.info())输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 14332 entries, 0 to 14331
Data columns (total 2 columns):
reqid    14332 non-null object
dni      14332 non-null object
the_max_df = the_big_df.join(df_to_join,on='reqid')
ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat

共有1个答案

孙恩
2023-03-14

这里的问题是对联接工作方式的误解:当您说the_big_df.join(df_to_join,on='reqid')时,它并不意味着像乍一看所假设的那样在the_big_df.reqid==df_to_join.reqid上联接,而是在the_big_df.reqid==df_to_join.index上联接。由于requid类型为object,而索引类型为int64,因此会出现错误。

请参见join的文档:

在索引上或键列上与其他数据表连接列。
...
on:str、str列表或类数组,可选
调用方中的列或索引级别名称,以连接其他中的索引,否则连接索引上的索引。

请看下面的示例

df1 = pd.DataFrame({'id1': [1, 2], 'val1': [11,12]})
df2 = pd.DataFrame({'id2': [3, 4], 'val2': [21,22]})
print(df1)
#   id1  val1
#0    1    11
#1    2    12
print(df2)
#   id2  val2
#0    3    21
#1    4    22

# join on df1.id1 (int64) == df2.index (int64) 
print(df1.join(df2, on='id1'))
#   id1  val1  id2  val2
#0    1    11  4.0  22.0
#1    2    12  NaN   NaN

# now df3 same as df1 but id3 as object:
df3 = pd.DataFrame({'id3': ['1', '2'], 'val1': [11,12]})

# try to join on df3.id3 (object) == df2.index (int64) 
df3.join(df2, on='id3')
#ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat
>>> df3.join(df2, on='id3')
  id3  val1  id2  val2
0   1    11  NaN   NaN
1   2    12  NaN   NaN
 类似资料:
  • 我有两个列表,我想离开加入一个。 当我尝试这样做时,我会得到同样的错误: 运行dtypes将两个列表标识符作为Object返回。

  • 我正在尝试根据两个数据页中共享的“别名编号”列名合并两个数据页。 以下是我所写的内容:

  • 我目前面临着一个问题,在使用Pandas处理和操作数据帧方面,我似乎无法解决这个问题。 为了让您了解我正在谈论的数据帧以及您将在我的代码中看到的数据帧: 我正在尝试将数据集“data”的“exercise”列中的单词更改为数据集“exercise”的“name”列中的单词。 例如,数据数据集锻炼列中的首字母缩略词Dl应更改为锻炼数据集名称列中的死电梯。 我尝试过许多方法,但似乎都失败了。我每次都收

  • 问题内容: 这是我保存在两个变量中的两个数据框: 我正在尝试使用以下代码合并这两个: 添加how =’left’的原因是,我的ranking_df中的数据点少于标准df中的数据点。 预期的行为是这样的: 但是我得到这个错误: ValueError:您正在尝试合并object和int64列。如果要继续,则应使用pd.concat 但是我不希望使用concat,因为我想合并树而不只是添加它们。 我想到

  • 问题内容: 我必须在这里缺少一些东西…我有以下代码和输出。您能看到为什么未从其中的每本书的类别集中删除吗? 谢谢!! 码: 输出: PS类别包含: Java文档 去掉 布尔值remove(Object o) 如果存在指定元素,则从该集合中删除该元素(可选操作)。更正式地讲,如果此集合包含这样的元素,则删除元素(e == null?e == null:o.equals(e))。如果此集合包含元素(或

  • a=[78,187,30] b=[78,186,185,25,30] c=[78,187,186,185,25,30] //想获得的结果 a=[1,2,3,4,5] b=[1,6,7,8,3,9,5] c=[1,2,6,7,8,3,4,9,5] //想获得的结果 a、b数组里面的值都是唯一的,怎么用js获得想要的值呢? 问了ChatGPT都没解决,它给的方法在控制台输出结果不一致,因为chatGP