如果在数据帧中找到行的列值,请从该行中删除该行

2024-04-18 06:06:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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中找到,并且它们对应的单位相同,那么从{}中删除整个凭证行。在

因此,在这种情况下,期望的输出是:

^{pr2}$

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


Tags: 数据方法目标data情况单位somedf1
3条回答

使用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

使用mergeindicator,在我们得到需要删除的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

虽然我们有很多好的答案,但问题似乎很有趣,因此作为学习,我承认这是非常有兴趣的,并想提出另一个版本,它看起来有点简单,使用布尔表达式:

第一个数据帧:

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

第二个数据帧:

^{pr2}$

可能更简单的答案:

>>> 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

相关问题 更多 >