I have the following datafame:
import numpy as np
import pandas as pd
df = {'ID': ['1','1','2', '2', '3', '3', '4', '4', '4'],
'USER' : ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'C'],
'DATE_VIEW': ['16/05/2019','18/05/2019', '16/03/2020', '18/03/2020', '16/07/2020', '21/07/2020', '13/02/2020', '14/02/2020', '15/02/2020'],
'DATE_ACCEPT': ['17/05/2019', np.nan, np.nan, '18/03/2020', '16/07/2020', np.nan, np.nan, '14/02/2020', np.nan],
}
df = pd.DataFrame(df)
df['DATE_VIEW'] = pd.to_datetime(df['DATE_VIEW'], format = '%d/%m/%Y')
df['DATE_ACCEPT'] = pd.to_datetime(df['DATE_ACCEPT'], format = '%d/%m/%Y')
df
I am looking for a way to keep for unique df['ID'] the row if the df['DATE_VIEW'] is smaller than the df['DATE_VIEW'] when the df['DATE_ACCEPT] has been populated and drop the row it if the df['DATE_VIEW'] is grater than the df['DATE_VIEW'] when the df['DATE_ACCEPT] has been populated for that particular df['ID']. Expected output below:

