简体   繁体   English

如何根据一组条件在 PANDAS 中创建一个新列,然后将新列设置为另一个字段的值

[英]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:这里有一些解决方案:

np.where np.哪里

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

Dataframe.where 数据框.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:我建议你看看这些链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据另一列中是否满足一组条件向 Python 中的数据框添加新列? - How can I add a new column to a dataframe in Python based on whether a set of conditions are met in another column? 如何根据其他 pandas 列和关联的字符串列的最大值创建新的 pandas 列? - How can I create a new pandas column based on the max value of other pandas columns and the associated string column? 如何使用基于上一行和下一行的条件在 Pandas Dataframe 上创建新列? - How can I create a new column on a Pandas Dataframe with conditions based on previous and next row? 如何根据熊猫中其他列的条件创建新列 - How to create a new column based on conditions on other column in pandas 根据另一列的值在熊猫中创建新列 - Create new column in pandas based on value of another column Pandas:根据另一列的文本值创建新列 - Pandas : Create new column based on text value of another column Python Pandas 根据另一个列值创建新列 - Python Pandas create new column based on another column value 根据 pandas 中另一列的值创建一个新列 - Create a new column, based on the value of another column in pandas Pandas - 根据另一列的条件值创建新列 - Pandas - create new column based on conditional value of another column Pandas:创建新列,根据条件从另一行查找和选择值 - Pandas: Create new column that finds & selects value from another row based on conditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM