Skip to content
Advertisement

Delete rows with date’s before the required date point based on key value

I have a pd.dataframe that looks like this:

key_value     date
value_01   2017-01-13
value_01   2018-02-17
value_01   2018-04-02
value_01   2018-05-13
value_01   2018-05-16  
value_02   2017-01-18
value_02   2018-03-13
value_02   2018-04-01
value_02   2018-05-16  
value_02   2018-05-22  
value_03   2018-01-13
value_03   2018-04-14

So now based on the key_value,

I want to drop all the rows that have their date column value before 2018-04-01

I want to have an end output like this:

 key_value     date
value_01   2018-04-02
value_01   2018-05-13
value_01   2018-05-16  
value_02   2018-04-01
value_02   2018-05-16  
value_02   2018-05-22  
value_03   2018-04-14

Advertisement

Answer

You can just filter your dataframe using Boolean indexing. There is no groupwise operation here. Just remember to convert your series to datetime first.

df['date'] = pd.to_datetime(df['date'])

res = df[~(df['date'] < '2018-04-01')]

print(res)

   key_value       date
2   value_01 2018-04-02
3   value_01 2018-05-13
4   value_01 2018-05-16
7   value_02 2018-04-01
8   value_02 2018-05-16
9   value_02 2018-05-22
11  value_03 2018-04-14
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement