Vectorize conditional assignment in pandas dataframe

Given a pandas dataframe, we have to vectorize conditional assignment in pandas dataframe. By Pranit Sharma Last updated : October 03, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

We are given a DataFrame df with some columns and we want to create a new column based on some previous columns.

We want to apply some conditions like if the value of a column is less then some specific value then the value of a new column is some new specific value. If the value of that column is some other specific value then the value of the new column would be some new specific value and so on.

Vectorize conditional assignment

We will use pandas.DataFrame.loc property of pandas so that we can access the exact element that fits the condition and we can set the value of a new column for each value of the old column.

The pandas.DataFrame.loc property is a type of data selection method which takes the name of a row or column as a parameter. To perform various operations using the pandas.DataFrame.loc property, we need to pass the required condition of rows and columns in order to get the filtered data.

Let us understand with the help of an example,

Python program to vectorize conditional assignment in pandas dataframe

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {'x':[0,-4,5,-2,2]}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original Dataframe:\n",df,"\n")

# Applying conditonal assignment
df['y'] = 0
df.loc[df['x'] < -2, 'y'] = 10
df.loc[df['x'] > 2, 'y'] = -10

# Display Result
print('Result:\n',df)

Output

The output of the above program is:

Example: Vectorize conditional assignment in pandas dataframe

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.