f0rd42 f0rd42 - 7 months ago 55
Python Question

python pandas add column content based on conditionals

I'm trying to add a column at a specific index based on the content of a different column:

df.insert(10, 'Escalation Status', np.where(df['Reasons for escalation'] == '', 'FALSE', 'TRUE'))

So, when Column "Reasons for escalation" is emtpy, write "FALSE", if not, write "TRUE" in the newly created "Escalation Status" column.

My problem: The content is always "TRUE", no matter if there is anything in the "Reasons for escalation" field.

example output:

Country,Date,Vendor,Product Family,Case number,Reseller Name,OpeningDate,Closing Date,Case Status,Topic,Escalation Reason,Reasons for escalation
DEU,28.10.2016,VENDOR,,201610281078864,,28.10.2016,28.10.2016,closed,Software issue,TRUE,Software
DEU,28.10.2016,VENDOR,,201610281078862,,28.10.2016,28.10.2016,closed,Config help,TRUE,

The first row is correct (Reasons for escalation is not empty, it contains "Software"), but the second row should be "FALSE", as the Reasons for escalation is empty



I think you need isnull for compare if NaN values:

df.insert(10, 'Escalation Status', 
          np.where(df['Reasons for escalation'].isnull(), 'FALSE', 'TRUE'))