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]]