Home > Mobile >  How can I create a new column in PANDAS based on a set of conditions and then setting the new column
How can I create a new column in PANDAS based on a set of conditions and then setting the new column

Time:12-24

I have a mock pandas dataframe 'df' where I want to create a new column 'fruit' and was wondering the easiest way to do this. The new column 'fruit_cost' will be taking the integer from the 'cost' column where the item type is equal to 'fruit'. What would the standard was of doing this in PANDAS be? Should I use conditional logic, or is there a simpler way. If anyone has any good practice tutorials for this type of thing it would also be beneficial.

enter image description here

In SQL I would create it using a case:

SQL

case
when item_type = 'fruit' then cost
else 0
end 
as fruit_cost 

*Python

import pandas as pd

list_of_customers =[
['patrick','lemon','fruit',10],
['paul','lemon','fruit',20],
['frank','lemon','fruit',10],
['jim','lemon','fruit',20], 
['wendy','watermelon','fruit',39],
['greg','watermelon','fruit',32],
['wilson','carrot','vegetable',34],    
['maree','carrot','vegetable',22],
['greg','','',], 
['wilmer','sprite','drink',22] 
]

df = pd.DataFrame(list_of_customers,columns = ['customer','item','item_type','cost'])

print(df)

#create new field 'fruit_cost'

df[fruit_cost] = if df[item_type] == 'fruit':
                    df[cost]
                 else:
                    0

CodePudding user response:

df["fruit_cost"] = df["cost"].where(df["item_type"] == "fruit", other=0)

CodePudding user response:

Here's some solutions:

np.where

df['fruit_cost'] = np.where(df['item_type'] == 'fruit', df['cost'], 0)

Dataframe.where

df['fruit_cost'] = df['cost'].where(df['item_type'] == 'fruit', 0)

There isn't really a standard since there are so many ways to do this; it's a matter of preference. I suggest you take a look at these links:

  • Related