马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import numpy as np
nba = np.genfromtxt("resources/nba.txt", encoding="utf-8", dtype=str) # 设置类型为 str,编码为 utf-8
print(nba)
[['排名' '队名' '胜' '负' '胜率' '胜场差' '主场' '客场' '赛区' '东部' '得分' '失分' '净胜' '连胜/负']
['1' '雄鹿' '46' '8' '85.2%' '0.0' '25-3' '21-5' '11-1' '28-4' '119.6'
'107.5' '12.1' '1连败']
['2' '猛龙' '40' '15' '72.7%' '6.5' '21-7' '19-8' '8-4' '29-8' '113.0'
'106.6' '6.3' '1连败']
['3' '凯尔特人' '37' '16' '69.8%' '8.5' '22-5' '15-11' '7-5' '25-9' '112.6'
'105.7' '6.8' '1连败']
['4' '热火' '35' '19' '64.8%' '11.0' '22-3' '13-16' '8-2' '23-7' '111.7'
'108.4' '3.4' '1连败']
['5' '76人' '34' '21' '61.8%' '12.5' '25-2' '9-19' '9-4' '22-14' '108.6'
'106.2' '2.4' '3连胜']
['6' '步行者' '32' '23' '58.2%' '14.5' '19-10' '13-13' '6-6' '20-15'
'109.7' '107.6' '2.2' '1连胜']
['7' '篮网' '25' '28' '47.2%' '20.5' '16-12' '9-16' '5-8' '17-16' '110.6'
'111.0' '-0.5' '2连胜']
['8' '魔术' '24' '31' '43.6%' '22.5' '15-13' '9-18' '8-4' '17-17' '103.9'
'105.4' '-1.5' '2连胜']
['9' '奇才' '20' '33' '37.7%' '25.5' '13-13' '7-20' '4-8' '14-19' '115.6'
'119.9' '-4.2' '2连胜']
['10' '公牛' '19' '36' '34.5%' '27.5' '11-16' '8-20' '6-8' '13-24' '106.4'
'109.5' '-3.1' '6连败']]
print(nba[:, [4, -2]])
# 拿出胜率(04 列)和净胜(倒数 -2 )
print(nba[:, [4, -2]])
[['胜率' '净胜']
['85.2%' '12.1']
['72.7%' '6.3']
['69.8%' '6.8']
['64.8%' '3.4']
['61.8%' '2.4']
['58.2%' '2.2']
['47.2%' '-0.5']
['43.6%' '-1.5']
['37.7%' '-4.2']
['34.5%' '-3.1']]
nba[:, [4, -2]][1:]
# 去掉第一行
print(nba[:, [4, -2]][1:])
[['85.2%' '12.1']
['72.7%' '6.3']
['69.8%' '6.8']
['64.8%' '3.4']
['61.8%' '2.4']
['58.2%' '2.2']
['47.2%' '-0.5']
['43.6%' '-1.5']
['37.7%' '-4.2']
['34.5%' '-3.1']]
print(np.char.strip(nba[:, [4, -2]][1:], '%'))
# 去掉第一列的 %
print(np.char.strip(nba[:, [4, -2]][1:], '%'))
[['85.2' '12.1']
['72.7' '6.3']
['69.8' '6.8']
['64.8' '3.4']
['61.8' '2.4']
['58.2' '2.2']
['47.2' '-0.5']
['43.6' '-1.5']
['37.7' '-4.2']
['34.5' '-3.1']]
# 将 str 类型转化为 float
print(np.char.strip(nba[:, [4, -2]][1:], '%').astype("f8")) # f8 是 float64 的缩写
[[85.2 12.1]
[72.7 6.3]
[69.8 6.8]
[64.8 3.4]
[61.8 2.4]
[58.2 2.2]
[47.2 -0.5]
[43.6 -1.5]
[37.7 -4.2]
[34.5 -3.1]]
|