Search code examples
pythondataframefillna

Filling in blanks based off of criteria of another


So what I am trying to achieve is basically fill in blank rows in a column based off of another columns. so here is a snippet of what my dataframe looks like.

Person_Name     State_Abbrev       Bool
george, John    CT                   NO
george, John    PA                   NO
george, John.   NY                   NO
adam, Ross      NY                   YES
adam, Ross      CA                   NO

So what I want to do is look at the Person_Name column and say if the bool column says no for each of that specific Person_Name and their are no Yes for that person then make a new column and fill in the column with the word question, but if the Person_Name does have at least one row for that specific person_name that has a yes then dont fill in anything for that person and move on to the next person in my dataframe.


Solution

  • You're looking for groupby + transform + isin:

    df['New'] = df.groupby('Person_Name').Bool\
            .transform(lambda x: 'Question' if ~x.isin(['YES']).any() else '')
    
    df
    
         Person_Name State_Abbrev Bool       New
    0   george, John           CT   NO  Question
    1   george, John           PA   NO  Question
    2  george, John.           NY   NO  Question
    3     adam, Ross           NY  YES          
    4     adam, Ross           CA   NO