ADVERTISEMENT
ADVERTISEMENT

Python Pandas: Conditional creation of a series/DataFrame column

Given a DataFrame, we have to create a conditional column in a DataFrame.
Submitted by Pranit Sharma, on May 04, 2022

Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. Sometimes we need to add a column to our dataset and this creation depends upon some condition. In pandas, we can achieve this task by using np.where() method.

pandas.np.where() Method

In this method, a condition is passed, and based on the condition it returns indices of elements in an input array which is also passed along with the condition.

Syntax:

np.where('condition' [x,y])

Parameter(s):

It takes a condition as a parameter, and two values, here X and Y, if the condition is true, it yields it value in X and otherwise Y.

To work with Pandas and numpy, we need to install pandas and numpy packages first, below is the syntax:

import numpy as np

import pandas as pd

Let us understand with the help of an example:

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = { 
    'Roll_no': [ 1,2,3,4,5],
    'Name': ['Abhishek', 'Babita','Chetan','Dheeraj','Ekta'],
    'Gender': ['Male','Female','Male','Male','Female'],
    'Marks': [50,66,76,45,80],
    'Standard': ['Fifth','Fourth','Third','Third','Third']
}

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

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

Output:

Example 1: Conditional creation of a series

Now we will add another column named "Status" and set it as "PASS" if marks are greater than 60 and "FAIL" if marks are less than 60.

# Importing pandas package
import pandas as pd
import numpy as np

# Creating a dictionary
d = { 
    'Roll_no': [ 1,2,3,4,5],
    'Name': ['Abhishek', 'Babita','Chetan','Dheeraj','Ekta'],
    'Gender': ['Male','Female','Male','Male','Female'],
    'Marks': [50,66,76,45,80],
    'Standard': ['Fifth','Fourth','Third','Third','Third']
}

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

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

# Creating a new column on the basis 
# of a condition
df['Status'] = np.where(df['Marks']>60, 'PASS','FAIL')

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

Output:

Example 2: Conditional creation of a series

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.