简体   繁体   English

使用if语句针对另一列在pandas数据框中创建新列

[英]Create new column in pandas dataframe using if statement against another column

I need to calculate a new column in my dataframe using a if in the calculation but have no luck with anything I have tried. 我需要在计算中使用if来计算数据框中的新列,但是我尝试过的任何方法都没有运气。 This is what I have 这就是我所拥有的

     OrderNum  Discount  OrderQty  UnitPrice  REMAININGQTY
0    115702       0.0      25.0     145.00          25.0
1    115793       0.0      20.0     823.00          20.0
2    115793       0.0      20.0     823.00          20.0
3    116282       0.0       1.0     699.95           1.0
4    116765       0.0      36.0     295.00           6.0

This is what I need. 这就是我所需要的。

column = PricePer 列= PricePer

the calculation = IIf(df.discount<>0,df.unitprice-(df.discount/df.orderqty),df.unitprice) 计算= IIf(df.discount<>0,df.unitprice-(df.discount/df.orderqty),df.unitprice)

How do I accomplish this? 我该如何完成?

Try: 尝试:

df['Price Per'] = pd.np.where(df.Discount != 0, df.UnitPrice - (df.Discount / df.OrderQty), df.UnitPrice)

Output: 输出:

  OrderNum  Discount  OrderQty  UnitPrice  REMAININGQTY  Price Per
0    115702       0.0      25.0     145.00          25.0     145.00
1    115793       0.0      20.0     823.00          20.0     823.00
2    115793       0.0      20.0     823.00          20.0     823.00
3    116282       0.0       1.0     699.95           1.0     699.95
4    116765       0.0      36.0     295.00           6.0     295.00

You might also use these 2 statements 您可能还会使用这2条语句

df.loc[df['Discount'] == 0, 'PricePer'] = df['UnitPrice']
df.loc[df['Discount'] != 0, 'PricePer'] = df['UnitPrice']-df['Discount']/df['OrderQty']

But if discount equals zero, the -(discount / orderqty) would be zero, no if is needed ? 但是,如果折扣等于零,则-(discount / orderqty)将为零,是否不需要? So I would think this might also do: 所以我认为这可能也可以:

df['PricePer'] = df['UnitPrice']-df['Discount']/df['OrderQty']

暂无
暂无

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

相关问题 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 使用另一个数据框创建熊猫数据框列 - Create pandas dataframe column using another dataframe 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 尝试根据与if语句相关的数据框在熊猫中创建新的数据框列 - Trying to create a new dataframe column in pandas based on a dataframe related if statement 尝试通过使用 if 语句过滤另一列在 pandas dataframe 中创建一个新列 - Trying to make a new column in pandas dataframe by filtering another column using a if statement Pandas 通过从另一个数据帧的 1 列中的单元格检查列表中返回匹配字符串的行来创建新的数据帧 - Pandas create a new dataframe by returning the rows matching strings from a list checked against cells in 1 column from an another dataframe 根据另一列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in another column 遍历一个数据框中的单个列与另一个数据框中的列进行比较使用熊猫在第一个数据框中创建新列 - loop through a single column in one dataframe compare to a column in another dataframe create new column in first dataframe using pandas 使用一个 Pandas 数据框填充另一个 Pandas 数据框的新列 - Using one pandas dataframe to populate new column in another pandas dataframe pandas:通过将 DataFrame 行与另一个 DataFrame 的列进行比较来创建新列 - pandas: Create new column by comparing DataFrame rows with columns of another DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM