嘿家伙試圖嘗試創建我自己的基本曲棍球分析程式。在嘗試獲取我提供的所有資訊時,我被這個錯誤難住了。嘗試查找一堆東西,但似乎沒有什么對我有用。我是使用 pandas 和 numpy 的新手。任何幫助將不勝感激,謝謝!
import numpy as np
nyr_sched = nhl2021_22_sched[(nhl2021_22_sched['home_team'] == 'NYR') | (nhl2021_22_sched['away_team'] == 'NYR')]
nyr_sched
for game in nyr_sched:
nyr_GID = nyr_sched['game_id']
nyr_GID = np.array(nyr_GID)
nyr_GID.tolist()
nyr_GID
哪個列印array([2021020004, 2021020011, 2021020023, 2021020035, 2021020059,...])
但是我在這里遇到了一個問題。
for i in nyr_GID:
nyr_GID[i] = nyr_GID[i][5:]
print("Game {} ID is {}".format(i, nyr_GID[i]))
我希望從串列中的每個 ID 中洗掉前 5 個數字,而202102004不是20004. 但是,我收到一條錯誤訊息,指出我正在嘗試訪問20212004索引。這是錯誤:
IndexError Traceback (most recent call last)
/var/folders/v8/z9h_xhmj1t38rzq1351q_my40000gn/T/ipykernel_13046/4170336391.py in <module>
1 count = 0
2 for i in nyr_GID:
----> 3 nyr_GID[i] = nyr_GID[i][5:]
4 print("Game {} ID is {}".format(i, nyr_GID[i]))
IndexError: index 2021020004 is out of bounds for axis 0 with size 82
uj5u.com熱心網友回復:
嘗試:
for i in range(len(nyr_GID)):
nyr_GID[i] = str(nyr_GID[i])[5:]
uj5u.com熱心網友回復:
我在
對于 nyr_GID 中的 i:
指 nyr_GID 中的元素。
要獲取相應的索引,請使用: range(nyr_GID.size)
微小的代碼片段:
test = np.array([234, 345, 456])
for i in test:
print(i, test[i])
Traceback (most recent call last): File "<stdin>", line 2, in <module>
IndexError: index 234 is out of bounds for axis 0 with size 3
#correct version
for i in range(test.size):
print(i, test[i])
0 234 1 345 2 456
在您的代碼中,您還需要
str(nyr_GID[i])將第 i 個元素轉換為字串
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/530249.html
標籤:Python熊猫麻木的
