[Python] Add pandas dataframe column based on whether two columns are null

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to add a column 'data_status' to a dataframe that contains 'data' if either 'total_pounds_entered' or 'total_pounds_estimated' is not null and 'no data' if both of these columns are null. The following code isn't working for me. I know this because I see two rows that have values in the 'total_pounds_estimated' column that end up in df instead of df_total_pounds_estimated_or_entered. Does anyone know what I'm missing here? Thanks!

    df['data_status'] = ''
    df[df['total_pounds_entered'].notnull()]['data_status'] = 'data'
    df[df['total_pounds_estimated'].notnull()]['data_status'] = 'data'
    df_total_pounds_estimated_or_entered = df[df['data_status'] == 'data']
    df = df[df['data_status'] != 'data']

    Continue reading...

Compartilhe esta Página