简体   繁体   English

如何制作 IF 并使用 Python (试过 IF 但没有用,也试过 np.where 但也没有用)

[英]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.

相关问题 为什么np.where函数也可以在值上工作 - Why does the np.where function also seem to work on values 在此示例中如何使np.where工作 - how to make np.where work in this example np.where 也检查多维数组中的子元素 - np.where checking also for subelements in multidimensional arrays 我尝试使用 python 进行 BTC 监控,但它不起作用 - I tried to make BTC monitoring using python but it doesn't work 我试图在 discord.py 中发出平衡命令,但没有奏效并出现意外错误 - I tried to make a balance command in discord.py but it didn't worked and gives unexpected errors 在python中使用np.where函数时如何避免NaN? - How to avoid NaN when using np.where function in python? 在python中,如何删除也使用换行符的打印中的最后一个字符? 试过 rstrip() - In python, how can I remove the last character in a print that is also using line continuation? Tried rstrip() Python:在嵌套列表中使用 np.where - Python: Using np.where in nested lists 如何编写这个 python 代码。 我试过但没有用 - How to write this python code. I have tried it but didn't work 如何处理Selenium python中弹出的证书操作系统? 我尝试了pyAutoGUI,但是没有用 - How to handle certificate OS pop up in Selenium python? I tried pyAutoGUI, but it didn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM