The DataFrame.where()
method in Python replaces values in a DataFrame where a specified condition is false. By default, it replaces empty fields with NaN
values.
Note: Pandas DataFrame is a two-dimensional labeled data structure.
# Signature according to Pandas documentationDataFrame.where(condition,other=NoDefault.no_default,inplace=False,axis=None,level=None,errors='raise',try_cast=NoDefault.no_default)
This method takes the following argument values:
condition
: Booleanother
: Series, Scalar, DataFrame, or callablecondition
gets false.inplace
: Boolean, default= False
axis
: Integer, default=None
level
: Integer, default=None
errors
: String, default=raise
raise
: It allows this method to raise exceptions.ignore
: It suppresses exceptions.try_cast
: Boolean, default=None
This method returns the same type as caller, or None
when inplace
gets True
in other cases.
Let's look at how we can create a DataFrame and filter databases on specified conditions using where()
.
# Importing the Pandas packageimport pandas as pd# Nested lists of datadata= [['Julia','Grade 10',78],['Butller','Grade 12',90],['Monitosh','Grade 11',88],['Butller','Grade 5',95],['vyohi','Grade 7',72]]# Creating a DataFramedf = pd.DataFrame(data, columns=['Name', 'Class','Marks'])# Creating Boolean series for Butller name_filter = df["Name"]=="Butller"# Filtering the extracted dataresults= df.where(_filter, inplace = False)# Showing the data on the consoleprint(results)
DataFrame()
method from the Pandas package to convert this nested list into a DataFrame of Name
, Class
, and Marks
. Butller
.df.where()
function to filter the student Buttler
's data. Here, the code is the same as above other than the filtering condition. Instead of one, we can also use multiple conditions using logical operators.
# Importing the Pandas packageimport pandas as pd# Nested lists of datadata= [['Julia','Grade 10',78],['Butller','Grade 12',90],['Monitosh','Grade 11',88],['Butller','Grade 5',95],['vyohi','Grade 7',72]]# Creating a DataFramedf = pd.DataFrame(data, columns=['Name', 'Class','Marks'])# Creating a Boolean series for the name Butller_filter1 = df["Name"]=="Butller"_filter2 = df["Class"]=="Grade 5"# Filtering the extracted dataresults= df.where(_filter1 & _filter2, inplace = False)# Showing data on the consoleprint(results)
Butller
.Grade 5
.Buttler
who studies in Grade 5
.