pandas 資料集中的 NDIM 是什麼?


ndim是 pandas 系列中用於獲取系列物件維度的整數表示的屬性。

眾所周知,pandas 系列是一個一維資料結構,所以這個 ndim 屬性的輸出始終為 1。它不需要任何輸入來獲取維度。無論行數和列數多少,ndim 屬性始終為 pandas 系列返回 1。

示例 1

在以下示例中,我們已將 ndim 屬性應用於 pandas series 物件“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)

輸出

輸出如下所示 −

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

正如我們在上述輸出塊中看到的輸出那樣,Series.ndim 屬性返回了一個值 1,表示給定 series 物件的維度為 1。

示例 2

讓我們建立一個帶有新元素的 pandas series 物件,然後應用 ndim 屬性獲取該 series 物件的維度。

# 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)

輸出

輸出如下 −

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

我們注意到 ndim 屬性為這兩個示例都返回輸出值 1。

更新時間:08-Mar-2022

774 次瀏覽

開啟你的 職業生涯

完成本課程來獲得認證

立即開始
廣告