Pandas | Count Number of Columns in a Dataframe

This article will discuss different ways to count the number of columns in a pandas dataframe in Python.

Table of Contents:

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.

Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top