[英]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.我有一个模拟熊猫数据框“df”,我想在其中创建一个新列“水果”,并且想知道最简单的方法来做到这一点。 The new column 'fruit_cost' will be taking the integer from the 'cost' column where the item type is equal to 'fruit'.
新列“fruit_cost”将从项目类型等于“fruit”的“cost”列中获取整数。 What would the standard was of doing this in PANDAS be?
在 PANDAS 中执行此操作的标准是什么? 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 中,我会使用一个案例来创建它:
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:这里有一些解决方案:
df['fruit_cost'] = np.where(df['item_type'] == 'fruit', df['cost'], 0)
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:
我建议你看看这些链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.