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

15,464

Solution 1

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

Solution 2

for future bypassers, here is a simple solution using an index.

idx_names = df_bulbr[df_bulbr.disconnected_time =='1970-01-01 00:00:00'].index
df_bulbr.drop(idx_names, inplace=True)
df_bulbr.reset_index()
Share:
15,464

Related videos on Youtube

user9996043
Author by

user9996043

Updated on June 04, 2022

Comments

  • user9996043
    user9996043 almost 2 years

    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