Python数据分析-NumPy模块-查看数组属性

155 阅读1分钟

查看数组的行数和列数

from numpy import array
a=array([[1,1],[2,2],[3,3]])
print(a.shape)

结果:
在这里插入图片描述
提取数组的行数或列数

from numpy import array
a=array([[1,1],[2,2],[3,3]])
print(a.shape)
print(a.shape[0])
print(a.shape[1])

结果:
在这里插入图片描述

查看数组的元素个数

from numpy import array
a=array([[1,1],[2,2],[3,3]])
print(a.size)

结果:
在这里插入图片描述

查看和转换数组元素的数据类型

from numpy import array
a=array([[1,1],[2,2],[3,3]])
b=array([[1.5,1],[2,2],[3,3]])
print(a.dtype)
print(b.dtype)

结果:
在这里插入图片描述
astype()函数进行数据类型转换

from numpy import array
a=array([[1,1],[2,2],[3,3]])
b=array([[1.5,1],[2,2],[3,3]])
b=b.astype(int)
print(a.dtype)
print(b.dtype)

结果:
在这里插入图片描述

查看数组的维数

from numpy import array
a=array([[1,1],[2,2],[3,3]])
b=array([1.5,1,2,2])
print(a.ndim)
print(b.ndim)

结果:
在这里插入图片描述