Dru
Dru

Reputation: 73

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

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.

Upvotes: 0

Views: 741

Answers (1)

sophocles
sophocles

Reputation: 13821

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

Upvotes: 1

Related Questions