Dimensions of a DataFrame

To get shape or dimensions of a DataFrame in Pandas, use the DataFrame.shape attribute. This attribute returns a tuple representing the dimensionality of this DataFrame.

The dimensions are returned as tuple (rows, columns).

In this tutorial, we will learn how to get the dimensionality of given DataFrame using DataFrame.shape attribute.

Examples

In the following program, we take a DataFrame and find its dimensionality using DataFrame.shape attribute.

Example.py

import pandas as pd

df = pd.DataFrame(
    {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]})

print(df.shape)
Try Online

Output

(3, 2)

In the following program, we take a DataFrame with two rows and one column. The shape attribute must return (2, 1) for this DataFrame.

Example.py

import pandas as pd

df = pd.DataFrame(
    {'name': ["apple", "banana"]})

print(df.shape)
Try Online

Output

(2, 1)

Conclusion

In this Pandas Tutorial, we learned how to the dimensions of a DataFrame using DataFrame.shape attribute.