Pandas Mask
To start we’re going to create a simple dataframe in python:
Prep the Data
To start we’re going to create a simple dataframe in python:
import pandas as pd import numpy as np
df = pd.DataFrame({ 'Hourly_Salary': ['500.00', '10000.00', '200.00', '20.00', np.nan] })
df['Hourly_Salary'] = pd.to_numeric(df['Hourly_Salary'])

Example 1 - if else state location
To start we’re going to create a simple dataframe in python:
df_mask = df.mask(df >= 1000)

Example 2 Keep under 1000, replace with other value - Doesnt fix null value
To start we’re going to create a simple dataframe in python:
df_mask_2 = df.mask(df >= 1000, other=999)

Example 3 fill null value
To start we’re going to create a simple dataframe in python:
df_mask_3 = df.mask(df.isnull(), 0)

Example 4 Column Example Replace anything over 100000 with null
To start we’re going to create a simple dataframe in python:
df2 = pd.DataFrame({ 'Running Back': ['Barry Sanders', 'Walter Payton', 'Emmitt Smith', 'Jim Brown'], 'Career Rushing Yards': [152690, 16726, 18355, 12312], 'Touchdowns': [99, 110, 164, 106] })

df2.mask(df2["Career Rushing Yards"] >= 100000)
Example 5 Multiple Conditions and
To start we’re going to create a simple dataframe in python:
df2.mask((df2["Touchdowns"] > 99) & (df2["Career Rushing Yards"] == 18355))

Example 6 Multiple Conditions or, filters outside
To start we’re going to create a simple dataframe in python:
filter1 = df2["Touchdowns"] > 108
filter2 = df2["Career Rushing Yards"] == 18355
df2.mask(filter1 | filter2)

Example 7 create new column, flag if a total is less
To start we’re going to create a simple dataframe in python:
df2["touchdown_totals"] = df2["Touchdowns"].mask(df2["Touchdowns"] < 100, other="Less Than 100")

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.