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

Time:04-08

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

CodePudding user response:

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

  • Related