Home > Enterprise >  Python - Pandas new DF column based on multiple conditions and null checks
Python - Pandas new DF column based on multiple conditions and null checks

Time:10-16

I am trying to add a new column to a dataframe based on several conditions. Below I have included how I would do this in SQL using a case statement but I keep running into walls with the Python/Pandas version.

SQL equivelent of what I hope to acheive:

select t.Apply_To,
       t.Comment_Code,

       Case when t.Apply_To is null and t.Comment_Code is null then 'Worked'
            when t.Apply_To is null then t.Comment_Code
            else t.Apply_To
       End as Hours

from table as t

Thanks in advance for any sugestions you may have.

CodePudding user response:

If you are willing to use numpy, try something as below with where:

import numpy as np

df['new_col'] = np.where( (df['Apply_To'].isnull()) & (df['Comment_Code'].isnull()),'worked', # when both Null
                         np.where(df['Apply_To'].isnull(),df['Comment_Code'],                 # when Apply_To is Null
                         df['Apply_To']))                                                     # else
  • Related