if __name__ =='__main__':# 文件名,分隔符,数据类型
nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1)print(nd01[0:3])======================================[['1986''Western Pacific''Viet Nam''Wine''0']['1986''Americas''Uruguay''Other''0.5']['1985''Africa'"Cte d'Ivoire"'Wine''1.62']]
6 ,取数据 : 行 ( 单行,多行 )
取第 3 行 : nd01[2]
if __name__ =='__main__':# 文件名,分隔符,数据类型
nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1)
res = nd01[2]print(res)==========================================================['1985''Africa'"Cte d'Ivoire"'Wine''1.62']
取 ( 2,3,4 ) 行 : nd01[1:4]
if __name__ =='__main__':# 文件名,分隔符,数据类型
nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1)
res = nd01[1:4]print(res)==============================================================[['1986''Americas''Uruguay''Other''0.5']['1985''Africa'"Cte d'Ivoire"'Wine''1.62']['1986''Americas''Colombia''Beer''4.27']]
7 ,取数据 : 列 ( 单列,多列 )
单列 : nd01[:,1]
if __name__ =='__main__':# 文件名,分隔符,数据类型
nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1)
res = nd01[:,1]print(res)print(type(res))================================================
结果 : 得到一个集合 ( 一维的 ndarray )['Western Pacific''Americas''Africa'....]
多列 : nd01[:,1:3]
if __name__ =='__main__':# 文件名,分隔符,数据类型
nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1)
res = nd01[:,1:3]print(res)print(type(res))======================================[['Western Pacific''Viet Nam']['Americas''Uruguay']['Africa'"Cte d'Ivoire"]...['Africa''Malawi']['Americas''Bahamas']['Africa''Malawi']]
9 ,取数据 : 多行多列 ( nd01[1:5,1:3] )
目的 : 1 ,从原始数据中提取数据 2 ,行号 : 2-5 行 3 ,列号 : 2-3 列
代码 :
if __name__ =='__main__':# 文件名,分隔符,数据类型
nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1)
res = nd01[1:5,1:3]print(res)print(type(res))==================================[['Americas''Uruguay']['Africa'"Cte d'Ivoire"]['Americas''Colombia']['Americas''Saint Kitts and Nevis']]
10 ,取元素 :nd01[1,2]
例如 :
if __name__ =='__main__':
nd01 = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
res = nd01[1,2]print(nd01)print(res)
11 ,创建 ndarray : 带默认值 np.full((2,4),1)
目的 : 创建 2 行 4 列的矩阵,默认值为 1
代码 :
if __name__ =='__main__':
nd01 = np.full((2,4),1,dtype="int")print(nd01)==========================[[1111][1111]]