This tutorial will discuss about different ways to select DataFrame rows where each column has equal values.
Table Of Contents
Preparing DataSet
Let’s create a DataFrame with some hardcoded data.
import pandas as pd data = {'Col_A': [21, 12, 33, 14, 35, 36, 17], 'Col_B': [21, 22, 33, 24, 25, 36, 27], 'Col_C': [21, 32, 33, 35, 35, 36, 35]} index = ['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7'] # Create a DataFrame from a dictionary df = pd.DataFrame.from_dict(data) # Set the index list as Index of DataFrame df.set_index(pd.Index(index), inplace=True) print (df)
Output
Col_A Col_B Col_C X1 21 21 21 X2 12 22 32 X3 33 33 33 X4 14 24 35 X5 35 25 35 X6 36 36 36 X7 17 27 35
We will now select rows from this DataFrame where each column has equal values.
Select DataFrame Rows with equal values in all columns
To select only those rows from the DataFrame, which contains the equal values in all the columns, we are going to apply a Lambda function on each row of the DataFrame. Inside the Lambda function, we will create a set containing the all the row values, and the if the size of set is 1, then it means all the values in that particular row are equal.
To apply a Lambda function on each row we are going to use the apply() method of DataFrame. We will pass this lambda function as first argument and axis=1
as the second argument in the apply() method. It will apply the Lambda function of each row of the DataFrame, and it will returns a boolean series, where each True
value in the boolean series represented that the particular row has equal values in all the columns.
Frequently Asked:
Then we will pass this boolean series into the loc[]
attribute of the DataFrame, and it will return a DataFrame containing only those rows which has same values in all the columns. Let’s see the complete example.
# Select rows with equal values in all columns subDf = df.loc[df.apply(lambda row: len(set(row)) == 1, axis=1) ] print (subDf)
Output
Col_A Col_B Col_C X1 21 21 21 X3 33 33 33 X6 36 36 36
Summary
We learned how to select DataFrame Rows where all values in the row are equal. Thanks.