mardi 3 septembre 2019

Populating a new column in a dataframe based on multi-level condititionals

I am trying to populate a column ['Mod_unMod'] based on multiple conditions from other columns in my dataframe ('cnms_df'). I wrote some pseudo-code to explain the difficulty:

IF (cnms_df['CA'] = "NO"):
    IF (cnms_df['I'] ="X" OR cnms_df['I']="V" OR cnms_df['I']="VE" OR cnms_df['I']="0.2 PCT ANNUAL CHANCE FLOOD HAZARD" OR cnms_df['I']="AREA NOT INCLUDED")
        cnms_df['Mod_unMod'] = "UnMapped"
    ELSE IF (LEFT(cnms_df['STUDY_TYPE].str[:3])="NON")
        cnms_df['Mod_unMod'] ="NON"

    ELSE IF (cnms_df['BJ']="X" OR cnms_df['BJ']="V" OR cnms_df['BJ']="V" OR cnms_df['BJ']="VE" OR cnms_df['BJ']="0.2 PCT ANNUAL CHANCE FLOOD HAZARD" OR cnms_df['BJ']="AREA NOT INCLUDED")   
        cnms_df['Mod_unMod']
    ELSE IF (LEFT(BK2,3)="NON" 
        cnms_df['Mod_unMod']="UnMod"
    ELSE:
        cnms_df['Mod_unMod']="Modernized")
ELSE:
    cnms_df['Mod_unMod'] = "UnMapped"    

I have applied simple np.where statements, but I am not sure how I could do this with the above levels. Is there a way to do something like this, in a logical fashion?

cnms_df['Mod_unMod'] = np.where((cnms_df['CA'] == 'No') & ((cnms_df['I'] ="X") | (cnms_df['I']="V") | (cnms_df['I']="VE") | (cnms_df['I']="0.2 PCT ANNUAL CHANCE FLOOD HAZARD") | (cnms_df['I']="AREA NOT INCLUDED")), "UnMapped", "Modernized")

Aucun commentaire:

Enregistrer un commentaire