[英]How To Make IF And using Python ( tried IF but didn't work , also tried np.where but also not worked)
Support needed as i want to make logic if and with python but i can't get the result i wanted or error appear Data frame i got is four column需要支持,因为我想使用 python 进行逻辑,但我无法得到我想要的结果或出现错误我得到的数据框是四列
1- first column under header (Segment) contain only one of two statement [ inbound ] or [outbound] 2- second column under header (Out_going Calls) contain number [ random number like 5,6,0,1,0] 3- Third column under header (POM_Count) contain number [ random number like 5,6,0,1,0] 4-Fourth column under header (Stuff_Time)contain number [ random number like 32400,5000,33660,20000,0] 1- header (Segment) 下的第一列仅包含两个语句 [inbound] 或 [outbound] 中的一个 2- header (Out_going Calls) 下的第二列包含数字 [random number like 5,6,0,1,0] 3- header (Stuff_Time) 下的第三列包含数字 [随机数,例如 5,6,0,1,0]
i need to make if and as shown below DF[Inbound_Call]=if (DF["segment =inbound"] & DF["Out_going Calls"] =0),0,DF["Stuff_Time"]我需要如下所示 DF[Inbound_Call]=if (DF["segment =inbound"] & DF["Out_going Calls"] =0),0,DF["Stuff_Time"]
DF[Outbound_Call]=if (DF["segment =Outbound"] & DF["POM_Count"] =0),0,DF["Stuff_Time"] DF[Outbound_Call]=if (DF["segment =Outbound"] & DF["POM_Count"] =0),0,DF["Stuff_Time"]
thanks i谢谢我
Staffed Time POM_Count Out_going Calls Segment
0 33240 0.0 0 Outbound
1 33237 0.0 10 NaN
2 33076 0.0 0 Outbound
3 37848 342.0 0 Inbound
4 38836 429.0 0 Outbound
.. ... ... ... ...
275 32671 146.0 0 Inbound
276 32497 149.0 10 Outbound
277 32663 139.0 13 Inbound
278 32787 238.0 4 Outbound
279 20918 45.0 0 Outbound
i tried that code but below error appear我尝试了该代码,但出现以下错误
PG["St_after_Ded"]=np.where(PG['Segment'] == 'inbound' & PG['Out_going Calls'] == 0, 0, PG["0"])
TypeError: Cannot perform 'rand_' with a dtyped [int64] array and scalar of type [bool]
The logic you decribed seems to be sufficiently appropriate to use with the np.where
, you just need to use the parentheses for each condition between the logic and.您描述的逻辑似乎足以与
np.where
一起使用,您只需为逻辑和之间的每个条件使用括号。
import pandas as pd
import numpy as np
PG = pd.read_csv('sample.csv')
PG["Inbound_Call"] = np.where(
(PG['Segment'] == 'Inbound') & (PG['Out_going Calls'] == 0), 0, PG['Staffed Time'])
PG["Outbound_Call"] = np.where(
(PG['Segment'] == 'Outbound') & (PG['POM_Count'] == 0), 0, PG['Staffed Time'])
print(PG)
Output from PG Output 来自PG
Staffed Time POM_Count Out_going Calls Segment Inbound_Call Outbound_Call
0 33240 0.0 0 Outbound 33240 0
1 33237 0.0 10 NaN 33237 33237
2 33076 0.0 0 Outbound 33076 0
3 37848 342.0 0 Inbound 0 37848
4 38836 429.0 0 Outbound 38836 38836
5 32671 146.0 0 Inbound 0 32671
6 32497 149.0 10 Outbound 32497 32497
7 32663 139.0 13 Inbound 32663 32663
8 32787 238.0 4 Outbound 32787 32787
9 20918 45.0 0 Outbound 20918 20918
I am not sure you expecting this, for condition with & operator you can use this,我不确定你是否期待这个,对于带有 & 运算符的条件,你可以使用它,
df.loc[(df['Segment'] == 'inbound') & (df['Out_going Calls'] == 0), 'Status'] = '0'
print(df)
Please provide the desired output format that you need.请提供您需要的 output 格式。 That may make task easier.
这可能会使任务更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.