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:
Output:
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.
Output:

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