Home > database >  How To Make IF And using Python ( tried IF but didn't work , also tried np.where but also not w
How To Make IF And using Python ( tried IF but didn't work , also tried np.where but also not w

Time:07-03

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

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]

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

CodePudding user response:

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. That may make task easier.

CodePudding user response:

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.

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

   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
  • Related