[Python] How can I append values to new columns based on computation of other columns in...

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    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().
    ​

    Continue reading...

Compartilhe esta Página