What is NDIM in the pandas Series?

PandasServer Side ProgrammingProgramming

The ndim is an attribute in the pandas series which is used to get the integer representation of dimensions of a series object.

As we know, the pandas series is a 1-dimensional data structure so the output for this ndim property is always 1. It doesn’t take any input to get the dimensions. Regardless of the number of rows and columns, the ndim property always returns 1 for pandas Series.

Example 1

In this following example, we have applied the ndim attribute to the pandas series object “s”.

# importing packages
import pandas as pd
import numpy as np

# create pandas Series
s = pd.Series(np.random.randint(1,100,10))

print("Series object:")
print(s)

# apply ndim property to get the dimensions
print('Result:', s.ndim)

Output

The output is given below −

Series object:
0    81
1    76
2    12
3    33
4    34
5    16
6    75
7    96
8    62
9    77
dtype: int32

Result: 1

As we can notice output in the above output block, the Series.ndim attribute has returned a value 1, which is indicating that the dimension of the given series object is 1.

Example 2

Let's create another pandas series object with new elements and then apply the ndim attribute to get the dimensions of the series object.

# importing packages
import pandas as pd

dates = pd.date_range('2021-01-01', periods=10, freq='M')

# create pandas Series
s = pd.Series(dates)

print("Series object:")
print(s)

# apply ndim property to get the dimensions
print('Result:', s.ndim)

Output

The output is as follows −

Series object:
0 2021-01-31
1 2021-02-28
2 2021-03-31
3 2021-04-30
4 2021-05-31
5 2021-06-30
6 2021-07-31
7 2021-08-31
8 2021-09-30
9 2021-10-31
dtype: datetime64[ns]

Result: 1

We can notice that the ndim attribute returned output value 1 for both examples.

raja
Updated on 08-Mar-2022 11:51:32

Advertisements