how to use where condition in dataframe code example

Example 1: pandas where

# replaces values with other where condition is False
DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)

import pandas as pd 
df = pd.DataFrame({'values':[1,2,3,4]})

df.where(df['values'] % 2 == 0, -1) # output : [-1, 2, -3, 4]

Example 2: new dataframe based on certain row conditions

# Create variable with TRUE if nationality is USA
american = df['nationality'] == "USA"

# Create variable with TRUE if age is greater than 50
elderly = df['age'] > 50

# Select all cases where nationality is USA and age is greater than 50
df[american & elderly]