【问题标题】:How to compare one dataframe with another and check whether the same data in first df present in second df如何将一个数据帧与另一个数据帧进行比较并检查第一个 df 中的相同数据是否存在于第二个 df 中
【发布时间】:2021-03-14 12:25:50
【问题描述】:

有两个 df.both df,一列同名

df1 有 40000 行,df 2 有 80000 行。

如何比较df1和df2中的数据是否相同。

预期输出:任何说明 df1 中的 40000 行与具有 80000 行的 df2 匹配的消息

40000 items in df1 matched with 80000 items in df2 


【问题讨论】:

标签: python python-3.x pandas dataframe pandas-groupby


【解决方案1】:

使用这个:

match = df1[df1['column name'].isin(df2['column name'])].shape[0]


print(('%.i items matched') % match)

【讨论】:

    【解决方案2】:

    类似:

    m = df1['c'] == df2['c']
    print('{0:d} items in df1 matched with {1:d} items in df2'.format(sum(m), len(m)))
    

    【讨论】:

      【解决方案3】:
      df = pd.DataFrame(data1, columns = ['A'])
      df2 = pd.DataFrame(data2, columns = ['A'])
      df
          A
      0  10
      1  15
      2  14
      3  20
      4  25
      5  26
      
       df2
          A
      0  10
      1  15
      2  14
      3  20
      4  25
      5  26
      6  30
      7  32
      8  34
      9  36
      
      
      
      df2[df2.A.isin(df.A.values)]
          A
      0  10
      1  15
      2  14
      3  20
      4  25
      5  26
      

      匹配的元素

      【讨论】:

      • 我猜这给出了不匹配的数据。
      • @hukkemaaruspetus 我确信这个返回匹配,而df2[~df2.A.isin(df.A.values)] 返回不匹配的数据
      【解决方案4】:

      试试:

       matches = (df2 == df1).stack()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-26
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 2019-06-23
        相关资源
        最近更新 更多