ndim 是 pandas 系列中的一个属性,用于获取系列对象维度的整数表示。
众所周知,pandas 系列是一维数据结构,因此该 ndim 属性的输出始终为 1。获取维度不需要任何输入。无论行数和列数如何,对于 pandas Series,ndim 属性始终返回 1。
示例 1
在以下示例中,我们将 ndim 属性应用于 pandas 系列对象“s”。
# importing packages import pandas as pd import numpy as np # create pandas Series s = pd.Series(np.random.randint(1,100,10)) print("系列对象:") print(s) # apply ndim property to get the dimensions print('Result:', s.ndim)输出结果
输出如下 -
系列对象: 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,这表示给定系列对象的维度是 1。
示例 2
让我们用新元素创建另一个 pandas 系列对象,然后应用 ndim 属性来获取系列对象的尺寸。
# 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("系列对象:") print(s) # apply ndim property to get the dimensions print('Result:', s.ndim)输出结果
输出如下 -
系列对象: 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。