2017-09-06 27 views
2

我的數據幀我與看起來像這樣工作後:熊貓:降排在一列中的值首次出現的每個ID

ID Alarm 
1 1 
1 2 
1 3 
2 3 
2 1 
2 2 
2 4 
3 4 
3 2 

我想單獨刪除對每個ID的所有行,第一次出現後,的報警= 2.所以輸出應該是:

ID Alarm 
1 1 
1 2 
2 3 
2 1 
2 2 
3 4 
3 2 

做什麼最簡單的方法是?這種情況下可能有熊貓方法嗎?我試着用locdf.loc[: df[(df['Alarm'] == 2)].index[0], :])做一些事情,但它會在第一次發生Alarm = 2事件後刪除所有行,與ID無關。

回答

2

您可以使用boolean indexingmask創建自groupby與自定義功能。

對於每個組首先比較shift值,因爲需要包括第一個2。然後通過比較eq。但是也需要匹配每個組下的2多個值,因此需要cumsum並與0比較 - False行被刪除。

df = df[df.groupby('ID')['Alarm'].apply(lambda x: x.shift().eq(2).cumsum().eq(0))] 
print (df) 
    ID Alarm 
0 1  1 
1 1  2 
3 2  3 
4 2  1 
5 2  2 
7 3  4 
8 3  2 

爲了更好地理解更多的數據:

df['a'] = df.groupby('ID')['Alarm'].apply(lambda x: x.shift()) 
df['b'] = df.groupby('ID')['Alarm'].apply(lambda x: x.shift().eq(2)) 
df['c'] = df.groupby('ID')['Alarm'].apply(lambda x: x.shift().eq(2).cumsum()) 
df['d'] = df.groupby('ID')['Alarm'].apply(lambda x: x.shift().eq(2).cumsum().eq(0)) 
print (df) 
    ID Alarm a  b c  d 
0 1  1 NaN False 0 True 
1 1  2 1.0 False 0 True 
2 1  2 2.0 True 1 False 
3 1  2 2.0 True 2 False 
4 1  3 2.0 True 3 False 
5 2  3 NaN False 0 True 
6 2  1 3.0 False 0 True 
7 2  2 1.0 False 0 True 
8 2  4 2.0 True 1 False 
9 3  4 NaN False 0 True 
10 3  2 4.0 False 0 True 
+0

@ jezrael,能否請您詳細闡述在幾個lines.That上面的代碼將是巨大的help.Thanks。 – Satya

+0

@Satya - 給我一些時間。 – jezrael

+1

@Satya - 請檢查編輯。 – jezrael

相關問題