Count the Rows and Columns in a Pandas Dataframe [Step-By-Step]

How To Get The Total Number Of Rows And Columns In A Dataframe

Hello Learner! In this article, we will be learning different ways of getting the total number of rows and columns of a pandas data frame. So let’s begin.

Also read: How to convert a Pandas dataframe to Numpy array?

Introduction

DataFrame in python is a two-dimensional, tabular data structure having numbers of rows and columns, containing different features. It is similar to a spreadsheet.

We can either create our own data frame using different objects of python such as lists or dictionaries or we can use the already available dataset in .csv format. In this article, we will be creating our own data frame.

For that, we need to install the pandas library of python and then import it whenever needed. Use the pip package manager to install Pandas

pip install pandas

Different Ways to Count the Rows and Columns in a Pandas Dataframe

Our aim here is to count the number of rows and columns in a given dataframe. So let’s begin.

1. Using the len() method with axes attribute

Here, we will be using the len() method to get the total count of rows and columns. DataFrame.axes[0] gives the count for rows and DataFrame.axes[1] prints the count of columns.

Let’s see an example:

#importing pandas
import pandas as pd

#creating dataframes
student_data = {"Name": ['Alice', 'Sam', 'Kevin', 'Max', 'Tom'],
        "exam_no": [201, 202, 203, 204, 205],
        "Result": ['Pass', 'Pass', 'Fail', 'Pass', 'Fail']}

#printing our dataframe
df1 = pd.DataFrame(student_data)
print(df1)

print("\n Total number of rows :", len(df1.axes[0]))
print("\n Total number of columns :", len(df1.axes[1]))

Our dataframe here consists of student data: their name, exam number, and their result. The output is :

Dataframe

2. Using the shape attribute

The shape[] attribute can be used to know the shape/dimension of our data frame, and the total number of rows and columns in it. The shape attribute of the data frame is used the same way we have used axes[] above.

DataFrame.shape[0] gives the count of rows and DataFrame.shape[1] gives the count of columns.

Considering the same example, let’s see how to use shape[]

print("\n Dimension of dataframe :", df1.shape)

print("\n Total number of rows :", df1.shape[0])

print("\n Total number of columns :", df1.shape[1])

The Output is :

Dimension of dataframe : (5, 3)

Total number of rows : 5

Total number of columns : 3

3. Using index and columns keywords

Similar to the above examples, here, the index keyword is used for getting the number of rows, and the column keyword is used to get the number of columns. Using the same example as above, let’s understand the use of these keywords:

print("\n Total number of rows :", len(df1.index))
print("\n Total number of columns :", len(df1.columns))

These lines of code will produce the same output as in the above cases:

Total number of rows : 5

Total number of columns : 3

Conclusion

So, In this article, we have seen all the ways to get the total count of rows and columns in our data frame. We’ve used the same example for all of the methods so that you can see how the syntax differs for each method while still generating the same result. Try these methods on your data frames and feel free to ask questions, if any.

Thank you! 🙂