Home > front end >  If Dataframe column A is X check if column B values are in list
If Dataframe column A is X check if column B values are in list

Time:11-10

I want to be able to create an error flag in a sperate column. I am not sure how to check conditions between two different DF columns.

Example DF

    Category Item    Flag
0   Fruit    Apple     
1   Fruit    Apple        
2   Fruit    Beef    
3   Fruit    Kiwi    
4   Fruit    Orange  

What I want to achive:

fruits = ['Apple', 'Orange', 'Kiwi']

if df['Category'] == 'Fruit'

check if df['Item'] is in fruits

if not append error

Expected output

    Category Item    Flag
0   Fruit    Apple   OK  
1   Fruit    Apple   OK     
2   Fruit    Beef    Error
3   Fruit    Kiwi    OK
4   Fruit    Orange  OK

CodePudding user response:

Using np.where()

df["Flag"] = np.where(df["Category"].eq("Fruit") & df["Item"].isin(fruits), "OK", "Error")

CodePudding user response:

This would do the trick for just fruits, creating a new df. You can loop through all Categories, and combine the dataframes if you want one dataframe.

temp_df = df[df['Category'] == "Fruit"]
temp_df['Flag'] = ['OK' if (x in fruits) else 'Error' for x in temp_df['Item']]

CodePudding user response:

You can do a simple df.apply to check

df["Flag"] = df.apply(lambda x: "OK" if x["Category"] == "Fruit" and x["Item"] in fruits else "ERROR", axis=1)

CodePudding user response:

  1. Create a function that checks both input values
  2. Use lambda function to apply it to each row.
def check(x, y):
    if y in fruits and x == "Fruit":
        return True
    else:
        return "Error"

df['Flag'] = df.apply(lambda x: check(x['Category'], x['Item']), axis=1)
  • Related