Skip to content Skip to sidebar Skip to footer

How To Use If Statements To Categorize With Multiple Conditions With Pandas

I have a categorization problem. The categorizing rule is: If Storage Condition == 'refrigerate' and 100 < profit Per Unit < 150 and Inventory Qty <20 is given, r

Solution 1:

Use np.where:

c1=df['Stroage Condition'].eq('refrigerate')
c2=df['Profit Per Unit'].between(100,150)
c3=df['Inventory Qty']<20
df['Restock Action']=np.where(c1&c2&c3,'Hold Current stock level','On Sale')
print(df)

   ID        Fruit Stroage Condition  Profit Per Unit   In Season orNot  Inventory Qty  \
01        Apple  room temperature               20                Yes           20012       Banana  room temperature               65                Yes            3023         Pear       refrigerate               60                Yes           18034   Strawberry       refrigerate              185                 No            7045   Watermelon  room temperature                8                 No            9056        Mango             Other               20                 No           10067  DragonFruit             Other               65                 No           105   

  Restock Action  
0On Sale  
1On Sale  
2On Sale  
3On Sale  
4On Sale  
5On Sale  
6On Sale  

In this case, no row verifies the 3 conditions, so for all rows the result is On Sale

Solution 2:

If you are not concerned with performance there is a convenience method called apply that can be used. It can take your function and apply it to either rows or columns in your dataframe.

It's good to know how it works and the downsides of using it if you plan on learning more about the pandas library.

When should I ever want to use pandas apply() in my code?

def func(df):
    ifdf['Stroage Condition'] == 'refrigerate' and 100 < df['Profit Per Unit'] < 150 and df['Inventory Qty'] < 20:
        return'Hold Current stock level'else:
        return'On Sale'df['Restock Action'] = df.apply(func, axis='columns')

Post a Comment for "How To Use If Statements To Categorize With Multiple Conditions With Pandas"