2016-02-29 58 views
1

我有一個熊貓數據框,它具有軌道上的信息以及它們如何匹配。有列了一大堆,但我想根據這些列砸行:是否存在「硬匹配」行具有相同的ID熊貓:根據不同的行值刪除行

 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 

如果要刪除「軟匹配」行:

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

所以在上面的例子205487000會在指數91和75被刪除,保留在指數47

回答

0

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

+0

這樣做。謝謝您的幫助。 – RedM