【问题标题】:How to match two dataframes precisely and get the output as 1 if matched and 0 if not matched?如何精确匹配两个数据帧,如果匹配则输出为 1,如果不匹配则输出为 0?
【发布时间】:2022-11-24 17:25:16
【问题描述】:

数据框如下:

df1:

name     |   age    |   state    |   number   | score
------------------------------------------------------
A            23         AZ         5434567        92.1
B            54         AZ         1234543        87.6
C            32         AZ         7654344        89.9
D            44         GA         8765433        72.4

df2:

name     |   age    |   state    |   number   | score
------------------------------------------------------
A            23         GA         5434567       92.1
D            54         AZ         1234543       76.4
C            33         AZ         7654344       99.9
D            46         GA         8765433       72.4

所需的数据框如下:

name     |   age    |   state    |   number   | score
-------------------------------------------------------
1            1            0            1          1
0            1            1            1          0
1            0            1            1          0
1            0            1            1          1

我试过的代码是:

outputdf = df1.eq(df2)

outputdf = df1.ne(df2)

但它们似乎都无法正常工作。

使用后输出错误当量线:

name     |   age    |   state    |   number   | score
-------------------------------------------------------
1            1            0            1          0
0            1            1            1          1
1            0            1            1          1
1            0            1            1          1

使用后输出错误线:

name     |   age    |   state    |   number   | score
-------------------------------------------------------
1            1            0            1          0
0            1            1            1          1
1            0            0            0          1
0            0            0            0          1

有人可以帮我吗? 谢谢

【问题讨论】:

  • But neither of them seem to work correctly - 你能解释更多吗?
  • @jezrael 只是匹配不正确。就像即使有匹配它输出 FALSE,反之亦然。
  • @Cosmo 您确定您的两个数据框具有相同的索引吗?
  • 你可以说得更详细点吗?换句话说,你能展示你真正的错误输出和预期输出吗?
  • 是的,我刚刚检查过。他们是一样的。 @南里

标签: python pandas dataframe match


【解决方案1】:

数据帧的直接比较应该有效,只需从 bool 转换为整数:

df1.eq(df2).astype(int)
# or (df1 == df2).astype(int)

输出:

   name  age  state  number
0     1    1      0       1
1     0    1      1       1
2     1    0      1       1
3     1    0      1       1

【讨论】:

  • 我仍然遇到完全相同的问题。就像即使有匹配它输出 FALSE,反之亦然。
  • 也许这是一个微妙的格式问题。你能用df1.to_dict()之类的更新吗?
【解决方案2】:

您的问题可能是由于浮点数近似引起的吗?

您可以在比较前对数字列进行四舍五入:

out = (df1.select_dtypes('number').round(2) # use the desired precision
 .eq(df2.select_dtypes('number').round(2))
 .astype(int)
)

输出:

   age  number  score
0    1       1      1
1    1       1      0
2    0       1      0
3    0       1      1

【讨论】:

    【解决方案3】:

    因为float columns precision problems is possible extract then and compare separately with numpy.isclose, 然后添加DataFrame.combine_first中的所有其他列:

    cols = df1.select_dtypes('floating').columns
    cols1 = df1.columns.difference(cols)
    
    df3 = pd.DataFrame(np.isclose(df1[cols], df2[cols]).astype(int), columns=cols)
    
    df3 = df3.combine_first(df1[cols1].eq(df2[cols1]).astype(int))
    

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多