[英]Dropping rows on a condition
我正在處理訂單處理數據集。 其中包含兩列,Order_ID和Transaction_Phase。 在訂購過程中,在首次訂購之前和訂購之后可以有幾個步驟。
在我當前的問題中,我想保留所有行,直到命中批准為止。 批准后的任何其他行均應刪除。 我只對批准之前發生的事情感興趣,因此批准后不需要任何信息。
Order_ID Tranaction_Phase
529334333 Quote
529334333 Deal approved
529334333 Rejected deal
470660845 Quote
470660845 Deal approved
470660845 Reject Deal
我希望我的輸出如下所示:
Order_ID Tranaction_Phase
529334333 Quote
529334333 Deal approved
4706608452 Quote
4706608452 Deal approved
誰能幫助我指引正確的方向:程序包,邏輯,文檔等。我正在使用python技術來實現這一目標。
df[df.index<=df.groupby('Order_ID')['Tranaction_Phase'].transform(lambda x:x.index[x=='Dealapproved'])]
Out[649]:
Order_ID Tranaction_Phase
0 529334333 Quote
1 529334333 Dealapproved
3 470660845 Quote
4 470660845 Dealapproved
In [36]: df.groupby('Order_ID', group_keys=False) \
.apply(lambda x: x.loc[:x['Tranaction_Phase'].eq('Deal approved').idxmax()])
Out[36]:
Order_ID Tranaction_Phase
3 470660845 Quote
4 470660845 Deal approved
0 529334333 Quote
1 529334333 Deal approved
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.