简体   繁体   中英

How can I create a new column in PANDAS based on a set of conditions and then setting the new column to the value of another field

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.

在此处输入图像描述

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
df["fruit_cost"] = df["cost"].where(df["item_type"] == "fruit", other=0)

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM