

Pandas – Check if all values in a Column are Equal
source link: https://thispointer.com/pandas-check-if-all-values-in-a-column-are-equal/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Pandas – Check if all values in a Column are Equal
This article will discuss how to check if all values in a DataFrame Column are the same.
First of all, we will create a DataFrame from a list of tuples,
import pandas as pd # List of Tuples students = [('jack', 34, 'Sydney', 'Australia', 100), ('Riti', 30, 'Delhi', 'India', 100), ('Vikas', 31, 'Mumbai', 'India', 100), ('Neelu', 32, 'Bangalore','India', 100), ('John', 16, 'New York', 'US', 100), ('Mike', 17, 'las vegas', 'US', 100)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Country', 'Budget'], index=['a', 'b', 'c', 'd', 'e', 'f']) # Display the DataFrame print(df)
Output:
Name Age City Country Budget a jack 34 Sydney Australia 100 b Riti 30 Delhi India 100 c Vikas 31 Mumbai India 100 d Neelu 32 Bangalore India 100 e John 16 New York US 100 f Mike 17 las vegas US 100
This DataFrame has six rows and five columns.
Check if all values are equal in a column
We can compare and check if all column values are equal to the first value of that column, then it means all values in that column are equal. The steps to do this is as follows,
Advertisements

- Select the column by name using subscript operator of DataFrame i.e. df[‘column_name’]. It gives the column contents as a Pandas Series object.
- Compare the Series object (selected column) with the first value. It will return a boolean Series.
- Check if all values in the boolean Series are True or not. If yes, then it means all values in the column are equal.
For example, let’s check if all values are the same in column ‘Budget’ from the above created DataFrame,
# Check if all values are same in column 'Budget' if (df['Budget'] == df['Budget'][0]).all(): print("All values are equal in column 'Budget'") else: print("All values are not equal in column 'Budget'")
Output:
All values are equal in column 'Budget'
We compared the first value of column ‘Budget’, with all the other column values and got a Boolean Series object. Then using the all() function of the Series object, we checked if all the values in Boolean Series are True or not. If all values are true, all values in that column are equal.
In this example, the ‘Budget’ column had equal values; therefore, the returned boolean Series had all True values and the Series.all() function returned True in this case. Let’s check out a negative example,
Let’s check if all values are equal in column ‘Age’ in the above created DataFrame,
# Check if all values are same in column 'Age' if (df['Age'] == df['Age'][0]).all(): print("All values are equal in column 'Age'") else: print("All values are not equal in column 'Age'")
Output:
All values are not equal in column 'Age'
In this example, the ‘Age’ column had different values; therefore, the returned boolean Series had some True and few False values, and the Series.all() function returned False in this case. It means that all values in column ‘Age’ are not equal.
Summary:
We learned about different ways to check if all the values in a DataFrame column are equal or not.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.
Join a LinkedIn Community of Python Developers
Recommend
-
10
Create a pandas data frame with the date index and the random values in the column advertisements How do I create a pandas dataframe with da...
-
12
Count Unique Values in a Column – thisPointer.comThis article will discuss different ways to Count unique values in a Dataframe Column in Python. First of all, we will create a sample Dataframe from a list of tuples i.e. ...
-
10
Pandas | Count non-zero values in Dataframe Column This article will discuss how to count the number of non-zero values in one or more Dataframe columns in Pandas. Let’s first create a Dataframe from a...
-
15
Pandas – Count True Values in a Dataframe Column In this article, we will discuss different ways to count True values in a Dataframe Column. First of all, we will create a Dataframe from a list of tuples...
-
11
Pandas: Check if all values in column are zeros This article will discuss checking if all values in a DataFrame column are zero (0) or not. First of all, we will create a DataFrame from a list of tuples,
-
14
Check if a Column exists in Pandas DataFrame In this article, we will discuss how to check if a column or multiple columns exist in a Pandas DataFrame or not. Suppose we have a DataFrame, ...
-
10
Check if all values in column are NaN in Pandas This article will discuss checking if all values in a DataFrame column are NaN. First of all, we will create a DataFrame from a list of tuples,
-
5
Remap values in Pandas Column with Dictionary In Pandas, A DataFrame is a two-dimensional array. Many times while working with pandas DataFrame, we need to remap the values of a specific column with dicti...
-
4
In this article, we will discuss various methods to replace the column values based on conditions in a pandas DataFrame. Let’s look at the table of contents describing the list of methods. Table of Contents
-
9
Check if all values in a Map are Equal in C++ This tutorial will discuss how to check if all values in a map are equal in C++. To check if all the values in a map are equal or not, we can first fetch the value o...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK