I have a pd.dataframe that looks like this:
JavaScript
x
14
14
1
key_value date
2
value_01 2017-01-13
3
value_01 2018-02-17
4
value_01 2018-04-02
5
value_01 2018-05-13
6
value_01 2018-05-16
7
value_02 2017-01-18
8
value_02 2018-03-13
9
value_02 2018-04-01
10
value_02 2018-05-16
11
value_02 2018-05-22
12
value_03 2018-01-13
13
value_03 2018-04-14
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:
JavaScript
1
9
1
key_value date
2
value_01 2018-04-02
3
value_01 2018-05-13
4
value_01 2018-05-16
5
value_02 2018-04-01
6
value_02 2018-05-16
7
value_02 2018-05-22
8
value_03 2018-04-14
9
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.
JavaScript
1
15
15
1
df['date'] = pd.to_datetime(df['date'])
2
3
res = df[~(df['date'] < '2018-04-01')]
4
5
print(res)
6
7
key_value date
8
2 value_01 2018-04-02
9
3 value_01 2018-05-13
10
4 value_01 2018-05-16
11
7 value_02 2018-04-01
12
8 value_02 2018-05-16
13
9 value_02 2018-05-22
14
11 value_03 2018-04-14
15