Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare dates between two columns within a certain number of days in pandas dataframe?

When there is an UNKNOWN, I would like to compare the dates of delivery column and test column to check if the delivery date is within 90 days of test date. If it is, print the delivery date. If it is not, move onto the next UNKNOWN until there are no more UNKNOWN.

data = {'car_part': ['100009','100093','100071','100033','100033','100043'],
        'car_number': ['UNKNOWN', 'X123-00027C', 'X123-00027C', 'UNKNOWN', 'X123-00148C', 'X123-00148C'],
        'delivery': ['11/20/2004', '12/17/2009', '7/27/2010', '11/1/2004', '9/5/2004', '11/10/2004'],
        'test': ['12/17/2004', '7/27/2010', '7/10/2020', '12/22/2006', '3/26/2007', '12/1/2007']}

df = pd.DataFrame(data)  

The expected result should show only 11/20/2004 printed

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

df["test"] = pd.to_datetime(df["test"])
df["delivery"] = pd.to_datetime(df["delivery"])
for index, row in df.iterrows():
    if row['car_number'] == "UNKNOWN":
        diff = (row['test']-row['delivery']).days
        if diff<91:
            print(row['delivery'])

it can be written much more efficient but I’m taking into account your level of python, it might be easier for your to learn it this way

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading