I have a dataset with date and id columns, and I want to set a third column 'Status' where if the date is before the value in the dict/lookup table, then 'On', 'Off' if on or after the date.
For example: dict = {'A':'1/2/2024',...} or look up table =
ID | Lookup date |
---|---|
A | 1/2/2024 |
B | 1/4/2024 |
To achieve:
Date | ID | Status |
---|---|---|
1/1/2024 | A | Off |
1/2/2024 | A | On |
1/3/2024 | A | On |
My current solutions involves duplicating the lookup values over the length of the df, but I want to do so without adding another column to prevent unnecessary memory use for big datasets with millions of rows:
df.merge(lookup,on='ID',how='left')
df.loc[df['Date']<df['lookup date'],'Status'] = 'Off'
df.loc[df['Date']>=df['lookup date'],'Status'] = 'On'