0

I wrote a loop to create a third column based on the values (dates) of two dataframe columns.

I have this dataframe :

'''

expiration_date administration_date
0   2021-02-26  2021-03-30
1   2021-11-28  2021-06-18
2   2021-11-23  2021-05-31
3   2021-11-28  2021-07-05
4   2021-11-28  2021-06-18

'''

I wrote this for loop to create the column "check" :

'''

df_test["check"] = "N/A"
for index, value in enumerate(df_test["expiration_date"]): 
    if value > df_test["administration_date"][index]:
        df_test["check"][index] = "OK"
    else:
        df_test["check"][index] = "expiration date < administration date" 

'''

It actually works :

'''

expiration_date administration_date check
0   2021-02-26  2021-03-30  expiration date < administration date
1   2021-11-28  2021-06-18  OK
2   2021-11-23  2021-05-31  OK
3   2021-11-28  2021-07-05  OK
4   2021-11-28  2021-06-18  OK

'''

But I was wondering if there is a more pythonic way to do it.

Many thanks !

Cesco83
  • 37
  • 1
  • 7
  • 1
    Use `df_test["check"] = np.where( df_test["expiration date"] < df_test["administration date"], 'expiration date < administration date', 'OK')` – jezrael Aug 17 '21 at 13:08

0 Answers0