Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对另一列使用if语句在dataframe中创建新列_Python_Pandas_Dataframe - Fatal编程技术网

Python 对另一列使用if语句在dataframe中创建新列

Python 对另一列使用if语句在dataframe中创建新列,python,pandas,dataframe,Python,Pandas,Dataframe,我需要在计算中使用if来计算数据帧中的一个新列,但我尝试过的任何操作都没有成功。这就是我所拥有的 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 2

我需要在计算中使用if来计算数据帧中的一个新列,但我尝试过的任何操作都没有成功。这就是我所拥有的

     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
这就是我需要的

列=价格

计算=
IIf(df.折扣0,df.单价-(df.折扣/df.订单数量),df.单价)

我如何做到这一点?

试试:

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

  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
尝试:

输出:

  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

您也可以使用这两条语句

df.loc[df['Discount'] == 0, 'PricePer'] = df['UnitPrice']
df.loc[df['Discount'] != 0, 'PricePer'] = df['UnitPrice']-df['Discount']/df['OrderQty']
但如果折扣等于零,则-(折扣/订单数量)将为零,是否需要?所以我想这也可以:

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

您也可以使用这两条语句

df.loc[df['Discount'] == 0, 'PricePer'] = df['UnitPrice']
df.loc[df['Discount'] != 0, 'PricePer'] = df['UnitPrice']-df['Discount']/df['OrderQty']
但如果折扣等于零,则-(折扣/订单数量)将为零,是否需要?所以我想这也可以:

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

df.discount0
是什么意思?同样在本节中,
折扣
始终为0,因此您总是以任何方式生成
单价
。在本例中,它是,但有时会有折扣。因此,它将从0.0变为0.25或任何折扣值。
将numpy作为np df['NEW VARIABLE']=np导入。其中([df['discount'!=0],df['UnitPrice']-(df['discount']/df['OrderQty'],df['UnitPrice'])
df.折扣0的含义是什么?同样在本节中,
折扣
始终为0,因此您总是以任何方式生成
单价
。在本例中,它是,但有时会有折扣。因此,它将从0.0更改为.25或任何折扣值。
将numpy作为np df[“新变量”]=np导入。其中([df[“折扣”!=0],df[“单价”]-(df[“折扣”]/df[“订单数量]),df[“单价”])
确保将numpy作为np导入,
将numpy作为np导入
实际上,当您使用pd.np时,您不需要导入numpy。。。。numpy是作为熊猫进口的一部分进口的。所以你可以做
pd.np.where…
也学到了一些新的东西。感谢您确保将numpy作为np导入,
将numpy作为np导入
实际上,当您使用pd.np时,您不需要导入numpy。。。。numpy是作为熊猫进口的一部分进口的。所以你可以做
pd.np.where…
也学到了一些新的东西。谢谢