代码之家  ›  专栏  ›  技术社区  ›  barciewicz

如果在另一个数据帧中找到某行的列值,则从该数据帧中删除该行

  •  4
  • barciewicz  · 技术社区  · 6 年前
    df1 = {
        'vouchers': [100, 200, 300, 400],
        'units': [11, 12, 12, 13],
        'some_other_data': ['a', 'b', 'c', 'd'],
        }
    df2 = {
        'vouchers': [500, 200, 600, 300],
        'units': [11, 12, 12, 13],
        'some_other_data': ['b', 'd', 'c', 'a'],
        }
    

    考虑到上面的两个数据帧,我想做以下操作:如果凭证来自 df1 可以在 df2 ,其对应单位相同,然后从中删除整个凭证行 DF1 .

    因此,在这种情况下,所需的输出将是:

    df1 = {
        'vouchers': [100, 300, 400],
        'units': [11, 12, 13],
        'some_other_data': ['a', 'c', 'd'],
        }
    

    实现这一目标的最佳方法是什么?

    6 回复  |  直到 6 年前
        1
  •  4
  •   cs95 abhishek58g    6 年前

    您可以使用索引操作有效地执行此操作,使用 pd.Index.isin :

    u = df1.set_index(['vouchers', 'units'])
    df1[~u.index.isin(pd.MultiIndex.from_arrays([df2.vouchers, df2.units]))]
    
       vouchers  units some_other_data
    0       100     11               a
    2       300     12               c
    3       400     13               d
    
        2
  •  3
  •   BENY    6 年前

    与做 merge indicator ,在我们得到 index 需要移除,使用 drop

    idx=df1.merge(df2,on=['vouchers','units'],indicator=True,how='left').\
         loc[lambda x : x['_merge']=='both'].index
    df1=df1.drop(idx,axis=0)
    df1
    Out[374]: 
       vouchers  units some_other_data
    0       100     11               a
    2       300     12               c
    3       400     13               d
    
        3
  •  2
  •   Karn Kumar    6 年前

    虽然我们有许多好的答案,但这些问题似乎很有趣,因此,我很感兴趣地承认这一点,并希望通过使用布尔表达式来放置另一个看起来不太简单的版本:

    第一个数据帧:

    >>> df1
       vouchers  units some_other_data
    0       100     11               a
    1       200     12               b
    2       300     12               c
    3       400     13               d
    

    第二个数据帧:

    >>> df2
       vouchers  units some_other_data
    0       500     11               a
    1       200     12               b
    2       600     12               c
    3       300     13               d
    

    可能更简单的答案:

    >>> df1[(df1 != df2).any(1)]
       vouchers  units some_other_data
    0       100     11               a
    2       300     12               c
    3       400     13               d
    

    解决方案2: 使用 merge + indicator + query

    >>> df1.merge(df2, how='outer', indicator=True).query('_merge == "left_only"').drop('_merge', 1)
       vouchers  units some_other_data
    0       100     11               a
    2       300     12               c
    3       400     13               d
    

    解决方案3:

    >>> df1[~df1.isin(df2).all(axis=1)]
       vouchers  units some_other_data
    0       100     11               a
    2       300     12               c
    3       400     13               d
    
        4
  •  1
  •   jpp    6 年前

    一种可能通过 pd.DataFrame.duplicated :

    df = pd.concat([df1, df2], ignore_index=True)
    df = df.loc[~df.duplicated(subset=['vouchers', 'units'], keep=False)]
    df = df.reindex(df.index & df1.index)
    
    print(df)
    
    #   some_other_data  units  vouchers
    # 0               a     11       100
    # 2               c     12       300
    # 3               d     13       400
    
        5
  •  0
  •   KaPy3141    6 年前

    我的解决方案:

    df1 = {
        'vouchers': [100, 200, 300, 400],
        'units': [11, 12, 12, 13],
        'some_other_data': ['a', 'b', 'c', 'd']
        }
    df2 = {
        'vouchers': [500, 200, 600, 300],
        'units': [11, 12, 12, 13],
        'some_other_data': ['a', 'b', 'c', 'd']
        }  
    
    y = 0
    for x in range(len(df1['vouchers'])):
        if df1['vouchers'][x-y] == df2['vouchers'][x]:
            if df1['units'][x-y] == df2['units'][x]:
                for key in df1.keys():
                    del df1[key][x]
                y += 1
    
        6
  •  0
  •   Abdullah    6 年前

    试试这个,很简单:

    excs = [] #will store the index of the values which are equal
    
    for i, (key, value) in enumerate(zip(df1["vouchers"], df1["units"])):
      for key2, value2 in zip(df2["vouchers"], df2["units"]):
        if key == key2 and value == value2:
          excs.append(i)
    
    for exc in excs:
      del(df1["vouchers"][exc])
      del(df1["units"][exc])