Number of Rows, Columns & Dimensions of pandas DataFrame in Python (3 Examples)

On this page you’ll learn how to count number of Rows & Columns of a pandas DataFrame in Python programming.

Setting up the Examples

import pandas as pd                        # Load pandas library
my_df = pd.DataFrame({'A':range(1, 5),    # Constructing data
                     'B':range(5, 1, - 1),
                     'C':range(6, 10)})
print(my_df)                              # Displaying example data
#    A  B  C
# 0  1  5  6
# 1  2  4  7
# 2  3  3  8
# 3  4  2  9

Example 1: Returning the Number of Rows of a pandas DataFrame

print(my_df.shape[0])                     # Using shape attribute
# 4

Example 2: Returning the Number of Variables of a pandas DataFrame

print(my_df.shape[1])                     # Using shape attribute
# 3

Example 3: Returning the Dimensions of a pandas DataFrame

print(my_df.shape)                        # Using shape attribute
# (4, 3)

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top