简体   繁体   中英

Drop rows based on date condition using python

I have a dataframe with, among others, a date and an ID column. Below is an example frame just for the purpose of this question. But the real data includes many more rows and columns.

from datetime import date, timedelta
import pandas as pd

date = datetime.datetime(2020, 1, 1)
delta_1 = 5
delta_2 = 15
delta_3 = 18

data = {
    'A': [date, date - timedelta(delta_1), date - timedelta(delta_2), date, date - timedelta(delta_3)], 
    'B': ['a', 'a', 'a', 'b', 'b']
}
df = pd.DataFrame(data)
print(df)

           A  B
0 2020-01-01  a
1 2019-12-27  a
2 2019-12-17  a
3 2020-01-01  b
4 2019-12-14  b

What I want to achieve is to, for each unique id (column B in the example), start with the most recent row, and drop rows based on a date condition: if a row with an existing id is inserted within 10 days from the most recent row with that id, it is only the latest row that is valid. So in this example, with 10 days as the limit, I would end up with this result:

           A  B
0 2020-01-01  a
2 2019-12-17  a
3 2020-01-01  b
4 2019-12-14  b

Any idea would be appreciated!

Here is one way , use diff with cumsum , get the day diff sum , then we get the divisor by //

s=df.groupby('B').A.apply(lambda x : x.diff().dt.days.cumsum().fillna(0).abs()//10)
df=df.groupby([df.B,s]).head(1)
           A  B
0 2020-01-01  a
2 2019-12-17  a
3 2020-01-01  b
4 2019-12-14  b

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