Filtering Pandas Dataframe Columns and Replacing Values Using a List Condition

Filtering Pandas Dataframe Columns and Replacing Values Using a List Condition

================================================================================================

This article will delve into the process of filtering specific columns in a pandas dataframe based on certain conditions and replacing values with new ones using a list. We’ll explore the various methods to achieve this, including using the isin() function, boolean indexing, and applying custom functions.

Introduction


The pandas library is a powerful tool for data manipulation and analysis in Python. One of its key features is the ability to filter specific columns based on certain conditions. In this article, we’ll explore how to achieve this using different methods, including filtering values that are not present in a list.

The Problem


Let’s consider an example dataframe:

IDTypeJob
1EmployeeDoctor
2Contingent WorkerDoctor
3EmployeeEmployee
4EmployeeEmployee
5Contingent WorkerEmployee
6Contingent WorkerConsultant
7Contingent WorkerTrainee
8Contingent WorkerSSS
9Contingent WorkerAgency Worker
10Contingent Worker

We have a list of acceptable values for employees who are classified as “Contingent Workers”:

list = ['Agency Worker', 'Consultant']

Our goal is to find the rows where the value in the “Type” column is “Contingent Worker” and replace any non-acceptable job values with “Consultant”.

Method 1: Using isin()


One way to achieve this is by using the isin() function, which checks if a series of values are present in a given list. We can use it to filter rows where the value in the “Job” column is not present in our list.

# Define the dataframe
import pandas as pd

df = pd.DataFrame({
    'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'Type': ['Employee', 'Contingent Worker', 'Employee', 'Employee', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker'],
    'Job': ['Doctor', 'Doctor', 'Employee', 'Employee', 'Employee', 'Consultant', 'Trainee', 'SSS', 'Agency Worker', '']
})

# Define the list of acceptable values
list = ['Agency Worker', 'Consultant']

# Filter rows where Job is not in the list and Type is Contingent Worker
df.loc[(df.Type == 'Contingent Worker') & (~df.Job.isin(list)), 'Job'] = 'Consultant'

In this method, we use a boolean mask to select rows where the value in the “Type” column matches our condition. We then apply another conditional statement to check if the value in the “Job” column is not present in the list using ~df.Job.isin(list). If it’s not present, we replace it with “Consultant”.

Method 2: Using Boolean Indexing


Another approach is by using boolean indexing. We can create a boolean mask based on our conditions and use it to select rows.

# Define the dataframe
import pandas as pd

df = pd.DataFrame({
    'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'Type': ['Employee', 'Contingent Worker', 'Employee', 'Employee', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker'],
    'Job': ['Doctor', 'Doctor', 'Employee', 'Employee', 'Employee', 'Consultant', 'Trainee', 'SSS', 'Agency Worker', '']
})

# Define the list of acceptable values
list = ['Agency Worker', 'Consultant']

# Create a boolean mask for filtering Type == Contingent Worker and Job not in list
mask = (df.Type == 'Contingent Worker') & (~df.Job.isin(list))

# Use boolean indexing to replace non-acceptable job values with Consultant
df.loc[mask, 'Job'] = 'Consultant'

In this method, we create a boolean mask using the same conditions as before and then use it to select rows. We apply the replacement operation directly on the dataframe without creating an intermediate series.

Method 3: Applying a Custom Function


We can also define a custom function that takes a row as input and returns True if the job value is not in our list, and False otherwise. Then, we use this function to filter rows.

# Define the dataframe
import pandas as pd

df = pd.DataFrame({
    'ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'Type': ['Employee', 'Contingent Worker', 'Employee', 'Employee', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker', 'Contingent Worker'],
    'Job': ['Doctor', 'Doctor', 'Employee', 'Employee', 'Employee', 'Consultant', 'Trainee', 'SSS', 'Agency Worker', '']
})

# Define the list of acceptable values
list = ['Agency Worker', 'Consultant']

def is_acceptable_job(job):
    return job in list

# Apply custom function to filter rows where Job is not in list and Type == Contingent Worker
df.loc[(df.Type == 'Contingent Worker') & ~df.Job.apply(is_acceptable_job), 'Job'] = 'Consultant'

In this method, we define a custom function is_acceptable_job that takes a job value as input and returns True if it’s in our list, and False otherwise. We then use the apply() method to apply this function to each row’s “Job” column. The boolean mask is created using the result of applying the custom function.

Conclusion


In this article, we explored different methods for filtering specific columns in a pandas dataframe based on certain conditions and replacing values with new ones using a list. We used isin(), boolean indexing, and applying custom functions to achieve this.

We hope that these examples help you understand how to filter specific rows in your dataframes efficiently.


Last modified on 2025-02-15