[Python] Fill nan values if another column value is not in list

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following dataframe:

    import pandas as pd
    import numpy as np
    df = pd.DataFrame({
    'Name': ['A','B','A','B','A','B','A','B'],
    'Include':[np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan],
    'Category':['Cat','Dog','Car','Dog','Bike','Dog','Cat','Bike'],
    })

    df


    [​IMG]

    I am trying to fill the Include column with the string yes if the column Category does not match the following list:

    exluded = ['Car','Bike']


    So that my expected output is this:

    [​IMG]

    Any ideas of how to achieve this?

    Continue reading...

Compartilhe esta Página