简体   繁体   中英

Pandas: Discard rows previous to value including value from another column

I want to discard every row previous to where Time is 0 containing the same ID as the row with the 0 -value. The row containing 0 shall also be deleted.

The data is as follows:

                          Time  Author         ID
Date
2018-04-23 08:09:52.558    60  1744025         44
2018-04-23 14:26:12.294   360  1244021         10
2018-04-23 15:19:47.667    45  1244021         10
2018-04-23 18:05:25.417   240  1249997         19
2018-04-23 18:58:20.776   180  2185555         19
2018-04-23 18:59:50.883   120  2185555         19
2018-04-23 19:29:30.500   300  1686620         19
2018-04-24 00:23:45.673     0  1249997         19
2018-04-24 06:55:29.529    10  1244021         10
2018-04-24 14:08:19.080   270  1686620         19
2018-04-24 17:58:30.757   120  1416825         39
2018-04-24 19:33:41.127   600  1249997         19

I want it to be:

                          Time  Author         ID
Date
2018-04-23 08:09:52.558    60  1744025         44
2018-04-23 14:26:12.294   360  1244021         10
2018-04-23 15:19:47.667    45  1244021         10
2018-04-24 06:55:29.529    10  1244021         10
2018-04-24 14:08:19.080   270  1686620         19
2018-04-24 17:58:30.757   120  1416825         39
2018-04-24 19:33:41.127   600  1249997         19

I fiddled around with idxmax() :

df[(df.Time == 0).idxmax():]

But that does not take the ID into account.

So how can I do that in the most "pythonic" way?

You can use a groupby + cumsum trick here:

df[~df.Time.eq(0)[::-1].groupby(df.ID, sort=False).cumsum()]

                         Time   Author  ID
Date                                      
2018-04-23 08:09:52.558    60  1744025  44
2018-04-23 14:26:12.294   360  1244021  10
2018-04-23 15:19:47.667    45  1244021  10
2018-04-24 06:55:29.529    10  1244021  10
2018-04-24 14:08:19.080   270  1686620  19
2018-04-24 17:58:30.757   120  1416825  39
2018-04-24 19:33:41.127   600  1249997  19

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM