Pandas DataFrame.where()-Python Last Updated : 24 Jun, 2025 Suggest changes Share Like Article Like Report DataFrame.where() function replace values in a DataFrame based on a condition. It allows you to keep the original value where a condition is True and replace it with something else e.g., NaN or a custom value where the condition is False. For Example: Python import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, -2, 3], 'B': [-1, 5, -6]}) res = df.where(df > 0) print(res) Output A B 0 1.0 NaN 1 NaN 5.0 2 3.0 NaN Explanation: df.where(df > 0) keeps values where the condition is True (greater than 0) and replaces others with NaN.SyntaxDataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)Parameters:ParameterDescriptioncond The condition to check (boolean DataFrame or Series)otherValue to replace where condition is False (default is NaN)inplaceIf True, modifies the DataFrame in placeaxisThe axis to align the condition alonglevelUsed with MultiIndexerrorsIf 'raise', errors are raised; if 'ignore', errors are ignoredtry_cast Try to cast the result to the original typeReturns: A DataFrame with replaced values (or None if inplace=True)Examples Example 1: In this, we are replacing negative numbers with 0 using the other parameter. Python import pandas as pd df = pd.DataFrame({'A': [1, -2, 3], 'B': [-1, 5, -6]}) res = df.where(df > 0, other=0) print(res) Output A B 0 1 0 1 0 5 2 3 0 Explanation: where(df > 0, other=0) keeps values > 0 and replaces negatives with 0, leaving positives unchanged.Example 2: In this, we are modifying the DataFrame directly to replace values ≤ 0 with NaN. Python import pandas as pd df = pd.DataFrame({'A': [2, -3, 4], 'B': [-5, 6, -7]}) df.where(df > 0, inplace=True) print(df) Output A B 0 2.0 NaN 1 NaN 6.0 2 4.0 NaN Explanation: inplace=True applies the change directly to df without needing to assign it to a new variable.Example 3: In this, we are applying a condition using a Series instead of the full DataFrame. Python import pandas as pd df = pd.DataFrame({'Score': [45, 85, 60, 30]}) condition = df['Score'] >= 50 res = df.where(condition, other='Fail') print(res) Output Score 0 Fail 1 85 2 60 3 Fail Explanation: Only scores ≥ 50 are kept and others are replaced with 'Fail'.Related article: DataFrame Advertise with us Next Article Pandas DataFrame.where()-Python K Kartikaybhutani Follow 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 Article Tags : Misc Python Python-pandas Python pandas-dataFrame Pandas-DataFrame-Methods +1 More Practice Tags : Miscpython Like