pythondataframeif-statement

How can I append values to new columns based on computation of other columns in Python?


I want to check if the date1 column is less than the other date2 column, then create a new column or New Column and assign 1, 0 otherwise. If the calculation is NaT then save NaT to the newly created column. Here is my trial.

if df['date1'] < df['date2'] == True:
    df['New Column'] = '1'
elif df['date1'] < df['date2'] == 'NaT':
    df['New Column'] = 'NaT'
else:
    df['New Column'] = '0'

But this does not seem to work and throwing

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


Solution

  • Reproducible data.

    df = pd.DataFrame({
        'date1':['2000-03-01', '2000-03-02'], 
        'date2':['2000-04-01', '2000-02-02'],
    })
    print(df)
            date1       date2
    0  2000-03-01  2000-04-01
    1  2000-03-02  2000-02-02
    

    First create your column setting the value to zero.

    df['New Column'] = '0'
    

    Then filter and set values:

    df.loc[df['date1'] < df['date2'], 'New Column'] = '1'
    print(df)
            date1       date2 New Column
    0  2000-03-01  2000-04-01          1
    1  2000-03-02  2000-02-02          0
    

    I'm not sure what you are trying to do with 'NaT' in this context. Perhaps applying isnat?