This article will discuss different ways to count the number of columns in a pandas dataframe in Python.
Table of Contents:
- Get number of columns in Dataframe using len() function
- Get number of columns in Dataframe using shape
- Get number of columns in Dataframe using size
Let’s first create a dataframe from a list of tuples i.e.
import pandas as pd # List of Tuples students = [('jack', 34, 'Sydeny', 'Australia'), ('Riti', 30, 'Delhi', 'India'), ('Vikas', 31, 'Mumbai', 'India'), ('Neelu', 32, 'Bangalore', 'India'), ('John', 16, 'New York', 'US'), ('Mike', 17, 'las vegas', 'US')] # Create a DataFrame object from list of tuples df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Country'], index=['a', 'b', 'c', 'd', 'e', 'f']) # Print the contents of the Dataframe print(df)
Contents of the dataframe are,
Name Age City Country a jack 34 Sydeny Australia b Riti 30 Delhi India c Vikas 31 Mumbai India d Neelu 32 Bangalore India e John 16 New York US f Mike 17 las vegas US
There are 4 columns in this Dataframe. Let’s see different ways to programmatically count the number of columns in this dataframe in Python.
Count the total number of columns in a Dataframe using len()
In Pandas, the dataframe has the attribute “columns”, which give an Index object containing the column Names. We can directly call the len() function with this Index object. It will provide us with the total number of columns in the dataframe. For example,
# Get total number of columns in a Dataframe num_of_columns = len(df.columns) print(num_of_columns)
Output:
4
As there were four columns in the dataframe, therefore we got the number 4.
Count the total number of columns in a Dataframe using shape
In Pandas, the dataframe provides an attribute shape. It returns a tuple representing the dimensions of the dataframe i.e., the number of rows and columns of the dataframe. We can fetch the value at index position one from this tuple, and it will give us the number of columns in the dataframe. For example
# Get total number of columns in a Dataframe num_of_columns = df.shape[1] print(num_of_columns)
Output:
4
As there were four columns in the dataframe, therefore we got the number 4.
Count the total number of columns in a Dataframe using the size attribute
In Pandas, the dataframe has the attribute ‘columns’, which give an Index object of column Names. We can use the ‘size’ attribute of this index object. It will provide the total number of columns in the dataframe. For example,
# Get total number of columns in a Dataframe num_of_columns = df.columns.size print(num_of_columns)
Output:
4
As there were four columns in the dataframe, therefore we got the number 4.
The complete working example is as follows,
import pandas as pd # List of Tuples students = [('jack', 34, 'Sydeny', 'Australia'), ('Riti', 30, 'Delhi', 'India'), ('Vikas', 31, 'Mumbai', 'India'), ('Neelu', 32, 'Bangalore', 'India'), ('John', 16, 'New York', 'US'), ('Mike', 17, 'las vegas', 'US')] # Create a DataFrame object from list of tuples df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Country'], index=['a', 'b', 'c', 'd', 'e', 'f']) # Print the contents of the Dataframe print(df) print('Count Total Number of Columns in a Dataframe') # Get total number of columns in a Dataframe num_of_columns = len(df.columns) print(num_of_columns) # Get total number of columns in a Dataframe num_of_columns = df.shape[1] print(num_of_columns) # Get total number of columns in a Dataframe num_of_columns = df.columns.size print(num_of_columns)
Output:
Name Age City Country a jack 34 Sydeny Australia b Riti 30 Delhi India c Vikas 31 Mumbai India d Neelu 32 Bangalore India e John 16 New York US f Mike 17 las vegas US Count Total Number of Columns in a Dataframe 4 4 4
Summary:
We learned about three different ways to count the total number of rows in the dataframe.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
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.