Count Rows & Columns of pandas DataFrame in Python (3 Examples) | How to Get the Dimensions
In this Python tutorial you’ll learn how to get the dimensions of a pandas DataFrame.
The post looks as follows:
It’s time to dive into the Python syntax…
Creating Example Data
Let’s first create some example data. For this, we first have to load the pandas library to Python.
import pandas as pd # Load pandas
Next, we can create an exemplifying pandas DataFrame as shown below:
data = pd.DataFrame({'x1':range(11, 17), # Create example data 'x2':range(10, 4, - 1), 'x3':["a", "c", "b", "a", "c", "a"], 'x4':range(1, 7)}) print(data) # Print example data # x1 x2 x3 x4 # 0 11 10 a 1 # 1 12 9 c 2 # 2 13 8 b 3 # 3 14 7 a 4 # 4 15 6 c 5 # 5 16 5 a 6
Have a look at the previous output: It shows the variables and rows of our DataFrame.
In the following examples, I’ll explain how to count the number of rows and columns of this DataFrame, and at the end I’ll show how to return all dimensions with a single line of Python code. So keep on reading!
Example 1: Get Number of Rows of pandas DataFrame
The following code explains how to identify the number of rows of a pandas DataFrame.
The Python programming language provides different alternatives for this task. The first method I want to show you is based on the shape attribute of our DataFrame:
print(data.shape[0]) # Using shape attribute # 6
The previous code has returned the value 6 to the console. In other words: Our DataFrame consists of six rows!
We can confirm this result by using the len function…
print(len(data)) # Using len function # 6
…or the columns attribute in combination with the count function:
print(data[data.columns[0]].count()) # Using columns attribute & count function # 6
As you can see, all of these methods lead to the same result, i.e. our pandas DataFrame is made of six rows.
So far so good – Let’s count the number of columns!
Example 2: Get Number of Columns of pandas DataFrame
This example illustrates how to count the variables of a pandas DataFrame.
For this task, we also can select between multiple approaches. The first method is also based on the shape attribute:
print(data.shape[1]) # Using shape attribute # 4
As you can see based on the previous output, our DataFrame is constructed of four columns.
We can confirm this result based on the len function and the columns attribute as shown below:
print(len(data.columns)) # Using len function & columns attribute # 4
Again, the result is 4!
Example 3: Get Dimensions of pandas DataFrame
You may have noticed in the previous examples, that we have used the shape attribute to find the number of rows AND the number of columns of our pandas DataFrame.
Example 3 shows how to use the shape attribute to find both dimension values at the same time!
For this, we simply have to use the shape attribute without indexing the result (as we did in the previous examples using square brackets).
Have a look at the following Python code and its output:
print(data.shape) # Using shape attribute # (6, 4)
As you can see, the console has returned the values 6 and 4, i.e. the number of rows and columns.
Video & Further Resources on the Topic
Have a look at the following video on my YouTube channel. In the video, I demonstrate the content of this tutorial and add some extra information:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Do you need more info on this tutorial and the handling of pandas DataFrames in Python? Then I can recommend having a look at the following video of Noureddin Sadawi’s YouTube channel.
In the video, he illustrates in another example how to return the dimensions of a data set in Python:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might have a look at the other tutorials of my website:
You have learned in this tutorial how to count the number of rows and columns of a pandas DataFrame in Python. Don’t hesitate to let me know in the comments, in case you have additional questions.