Python | Pandas DataFrame.where() Last Updated : 01 Dec, 2023 Comments Improve Suggest changes Like Article Like Report Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas where() method in Python is used to check a data frame for one or more conditions and return the result accordingly. By default, The rows not satisfying the condition are filled with NaN value. Pandas DataFrame.where() Function SyntaxSyntax: DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False, raise_on_error=None) Parameters: cond: One or more condition to check data frame for.other: Replace rows which don't satisfy the condition with user defined object, Default is NaN inplace: Boolean value, Makes changes in data frame itself if True axis: axis to check( row or columns) For the link to the CSV file used, Click here. Python Pandas DataFrame.where() ExamplesBelow are some examples of Pandas DataFrame.where(): Pandas DataFrame.where() Single Condition OperationIn this example, rows having particular Team name will be shown and rest will be replaced by NaN using .where() method. Python3 # importing pandas package import pandas as pd # making data frame from csv file data = pd.read_csv("nba.csv") # sorting dataframe data.sort_values("Team", inplace=True) # making boolean series for a team name filter = data["Team"] == "Atlanta Hawks" # filtering data data.where(filter, inplace=True) # display data Output As shown in the output image, every row which doesn't have Team = Atlanta Hawks is replaced with NaN. Pandas DataFrame.where() with Multiple Columns and ConditionsIn this example, data is filtered on the basis of both Team and Age. Only the rows having Team name "Atlanta Hawks" and players having age above 24 will be displayed. Python3 # importing pandas package import pandas as pd # making data frame from csv file data = pd.read_csv("nba.csv") # sorting dataframe data.sort_values("Team", inplace=True) # making boolean series for a team name filter1 = data["Team"] == "Atlanta Hawks" # making boolean series for age filter2 = data["Age"] > 24 # filtering data on basis of both filters data.where(filter1 & filter2, inplace=True) # display data Output As shown in the output image, Only the rows having Team name "Atlanta Hawks" and players having age above 24 are displayed. Comment More infoAdvertise with us Next Article Python | Pandas DataFrame.where() K Kartikaybhutani Follow Improve Article Tags : Misc Python Python-pandas Python pandas-dataFrame Pandas-DataFrame-Methods +1 More Practice Tags : Miscpython Similar Reads Python | Pandas Series.isnull() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.isnull() function detect missi 2 min read Python | Pandas dataframe.isna() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.isna() function is used to detect missing values. It return a boolean 2 min read Python | Pandas DataFrame.fillna() to replace Null values in dataframe Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Ju 5 min read Python | Pandas dataframe.clip() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.clip() is used to trim values at specified input threshold. We can us 3 min read Pandas DataFrame.columns In Pandas, DataFrame.columns attribute returns the column names of a DataFrame. It gives access to the column labels, returning an Index object with the column labels that may be used for viewing, modifying, or creating new column labels for a DataFrame.Note: This attribute doesn't require any param 2 min read Pandas Dataframe.sort_values() In Pandas, sort_values() function sorts a DataFrame by one or more columns in ascending or descending order. This method is essential for organizing and analyzing large datasets effectively.Syntax: DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last') 2 min read Python | Pandas Series.value_counts() Pandas is one of the most widely used library for data handling and analysis. It simplifies many data manipulation tasks especially when working with tabular data. In this article, we'll explore the Series.value_counts() function in Pandas which helps you quickly count the frequency of unique values 2 min read Python | Pandas DataFrame.nlargest() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas nlargest() method is used to get n largest values from a data frame or a series 2 min read Python | Pandas DataFrame.nsmallest() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas nsmallest() method is used to get n least values from a data frame or a series. 2 min read Python Pandas - DataFrame.copy() function The DataFrame.copy() function in Pandas allows to create a duplicate of a DataFrame. This duplication can be either a deep copy, where the new DataFrame is entirely independent of the original, or a shallow copy, where changes to the original data reflect in the copy. The main takeaway is that copy( 4 min read Like