如何获得熊猫系列的尺寸?



我有一个熊猫系列测试集y_test。我想知道它的大小用于统计。

使用此代码:

print(y_test.shape)

(31054,)

我想要得到31054

你可以选择

size = y_test.shape[0]

返回元组的第一个元素,即size。也可以直接使用size属性或len()方法获取。

size = y_test.size
#or
size = len(y_test)

y_test.shape给出一个元组。所以你可以执行print(y_test.shape)[0]来获取第一个元素,也就是序列的大小。

shape给了你一个元组,因为它来自dataframes, dataframes给了你帧的形状(行,列)。

使用lenSeries.size:

y_test = pd.Series(range(20))
print(len(y_test))
20
print(y_test.size)
20

最新更新