簡體   English   中英

熊貓:根據不同的行值刪除行

[英]Pandas: Drop row based on a different rows value

我有一個熊貓數據框,上面有關於賽道及其匹配方式的信息。 有很多列,但我想根據這些列刪除行:

     ID          Hard_Match  Soft_Match
75   205487000      False       True
91   205487000      False       True
47   205487000       True      False
0    209845000       True      False
62   210842000       True      False
81   212085000      False       True
96   229132000      False      False
90   229550000      False      False
66   229758000       True      False

如果要刪除“軟匹配”行(如果存在具有相同ID的“硬匹配”行):

for each row in dataframe:
    if row[hardmatched] == True somewhere else:
       remove row
    else:
       keep row

因此,在上述示例中,將在索引91和75中刪除205487000,並保留在索引47中。

IIUC然后執行您想要的操作:

In [112]:
ids = df.loc[df['Soft_Match'] == True, 'ID'].unique()
ids

Out[112]:
array([205487000, 212085000], dtype=int64)

In [113]:    
hard_matches = df.loc[(df['Hard_Match'] == True) & df['ID'].isin(ids), 'ID']
hard_matches

Out[113]:
47    205487000
Name: ID, dtype: int64

In [116]:
df.loc[~((df['ID'].isin(hard_matches)) & (df['Hard_Match'] == False))]

Out[116]:
           ID Hard_Match Soft_Match
47  205487000       True      False
0   209845000       True      False
62  210842000       True      False
81  212085000      False       True
96  229132000      False      False
90  229550000      False      False
66  229758000       True      False

因此,這首先查找“ Soft_Match”為True時的ID,然后找到“ Hard_Match”為True且ID與這些ID匹配的位置,然后過濾出ID匹配且“ Hard_Match”為False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM