How to Get Number of Axes in Pandas DataFrame?


Pandas DataFrame - Get Number of Axes

To get number of axes or array dimensions in a Pandas DataFrame, call DataFrame.ndim property. The DataFrame.ndim returns an integer representing the number of axes in this DataFrame.

Syntax

The syntax to call ndim property of a DataFrame is

DataFrame.ndim

Examples

1. Get the number of axes in given DataFrame

In this example, we have created a DataFrame and we shall get the number of axes in this DataFrame using DataFrame.ndim property.

Python Program

import pandas as pd

df = pd.DataFrame(
    [['abc', 22, 22.6],
    ['xyz', 25, 23.2],
    ['pqr', 31, 30.5]],
    columns=['name', 'age', 'bmi'])

number_of_axes = df.ndim
print(f'Number of axes in this DataFrame = {number_of_axes}')

Output

Number of axes in this DataFrame = 2

Since there are only two axes in our DataFrame, ndim property returned a value of 2.

Summary

In this tutorial of Python Examples, we learned how to get the number of axes in a DataFrame using DataFrame.ndim property.