Pandas add column with value based on condition based on other columns

Given a pandas dataframe, we have to add column with value based on condition based on other columns. 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

Suppose we are given a DataFrame with some columns like age and salary and we want to add an extra column called new which represents if a person is living a good life or not according to the salary.

Adding column with value based on condition based on other columns

We will simply use numpy.where() condition to check the problem and if the condition is satisfied, it will store in the column. This method returns the indices of elements in an input array where the given condition is satisfied.

Let us understand with the help of an example,

Python program to add column with value based on condition based on other columns

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating two dictionary
d = {
    'age':[29,28,21,30],
    'salary':[90000,80000,40000,650000]
}

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

# Display DataFrame
print("DataFrame1:\n",df,"\n")

# Applying condition and 
# checking for new column
df['new'] = np.where(df['salary']>=50000, 'yes', 'no')

# display result
print("Result:\n",df)

Output

The output of the above program is:

Example: Pandas add column with value based on condition based on other columns

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






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