Q:

Python Pandas: Conditional creation of a series/DataFrame column

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Selecting/excluding sets of columns in pandas... >>
<< How to filter pandas DataFrame by operator chainin...