Do nothing when condition fails pandas: np.where()

In this Section we will be focusing on how to write the condition in pandas using np.where(). Which do nothing when condition fails pandas only populate the values on the success condition and does nothing on the failure condition. Also populate the success condition and for failure condition use the existing columns in pandas. We have created examples for three cases

  • Do nothing when condition fails in pandas : np.where() populate nothing when condition fails
  • Do nothing when condition fails in pandas : df.loc () populate NA when condition fails
  • Do nothing when condition fails in pandas : np.where()  populate the success condition and for failure condition use the existing columns

 

Example :

 

#### do nothing when condition fails pandas
#attempt to create DataFrame

df=pd.DataFrame({'Name':['Sun','Mars','Earth','Moon','Pluto','Jupiter','Saturn'],
'diameter_in_kms': [np.nan, 6792,12756, 3475,2376,142984,120536],})

df

Created dataframe will be,

Do-nothing-when-condition-fails-pandas-np.where-1

 

 

np.where() populate nothing when condition fails in pandas:

Do Nothing on np.where(). only populate the success condition and do not populate anything on failure condition. On failure it will populate only “”.

 

###np.where

############ Do nothing : only populate the success condition and ignore failure conditions


df['Type'] = np.where(df['diameter_in_kms']>4000,'Planet','')
df

so the resultant dataframe will be

Do-nothing-when-condition-fails-pandas-np.where-2

 

 

Populate the success condition and for failure condition use the existing columns in pandas – np.where():

On np.where() populate the value only for the success condition and for failure condition populate using one of the existing column. In the below example we have populated the Column name “Type” with Planet when diameter is greater than 4000kms and on failure case the actual names will be populated  as shown below

 
##np where
############ Do nothing : populate the success condition and for failure condition use the existing columns


df['Type'] = np.where(df['diameter_in_kms']>4000,'Planet',df['Name'])
df

so the resultant dataframe will be

Do-nothing-when-condition-fails-pandas-np.where-2b

 

 

Do nothing when condition fails in pandas : df.loc () populate NA  when condition fails

On df.loc() populate the same value only for the success condition and for failure condition populate NA. In the below example we have populated the Column name “diameters” with Planet when diameter is greater than 4000kms and on failure case NA value will be populated as shown below

 

############ Do nothing : populate the success condition and for failure condition populate nothing


df['diameters'] = df.loc[df['diameter_in_kms']>4000, 'diameter_in_kms']
df

So the resultant dataframe will be

Do-nothing-when-condition-fails-pandas-np.where-3

 

 

Populate the success condition and for failure condition use the existing columns in pandas – pd.Series.mask ():

On pd.series.mask () populate the value only for the success condition and for failure condition populate using one of the existing column. In the below example we have populated the Column name “Type” with Planet when diameter is greater than 4000kms and on failure case the actual names will be populated as shown below

 
### pd.Series.mask updates a value when a condition is met
############ Do nothing : populate the success condition and for failure condition use the existing columns


df['Type']=df['Name'].mask(df['diameter_in_kms']>4000, 'Planet',inplace=False)
df

so the resultant dataframe will be

Do-nothing-when-condition-fails-pandas-np.where-4

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.