Pandas DataFrame ndim attribute with Examples

Spread the love

The Pandas DataFrame ndim attribute returns the number of dimensions of the DataFrame. It returns 1 of it is a series otherwise it returns 2 if it is a DataFrame.

Syntax –

dataframe.ndim

Let’s read a dataset.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv'
df = pd.read_csv(url)
df.head()

Now, Let’s check the number of dimensions of this dataframe.

df.ndim

Since it is a DataFrame the number of dimensions is 2.

Let’s create a series then check the number of dimensions.

net_sales = df['Net Sales']
print(type(net_sales))
print(net_sales.ndim)

Since it is a series the number of dimensions is 1.

Rating: 1 out of 5.

Leave a Reply