簡體   English   中英

Python:在 pd.DataFrame 中循環行時,“ValueError:只能將大小為 1 的數組轉換為 Python 標量”

[英]Python: 'ValueError: can only convert an array of size 1 to a Python scalar' when looping over rows in pd.DataFrame

我想遍歷 DataFrame 的行,在我的例子中計算多個運動隊的強度等級。

DataFrame 列'home_elo''away_elo'包含所涉及球隊的賽前實力評級(ELO 分數),並在比賽結束后在下一場主/客場比賽的行中更新(每支球隊在任何時候都有兩個實力評級)時間點,對於主客場比賽),以及update_elo(a,b,c)返回的內容。

相應的代碼片段如下所示:

for index in df.index:

    counter = counter + 1
    # Calculation of post-match ELO scores for home and away teams
    if df.at[index,'updated'] == 2: # Update next match ELO scores if not yet updated but pre-match ELO scores available

        try:
            all_home_fixtures = df.date_rank[df['localteam_id'] == df.at[index,'localteam_id']]
            next_home_fixture = all_home_fixtures[all_home_fixtures > df.at[index,'date_rank']].min()
            next_home_index = df[(df['date_rank'] == next_home_fixture) & (df['localteam_id'] == df.at[index,'localteam_id'])].index.item()
        except ValueError:
            print('ERROR 1 at' + str(index))
            df.at[index,'updated'] = 4

        try:
            all_away_fixtures = df.date_rank[df['visitorteam_id'] == df.at[index,'visitorteam_id']]
            next_away_fixture = all_away_fixtures[all_away_fixtures > df.at[index,'date_rank']].min()
            next_away_index = df[(df['date_rank'] == next_away_fixture) & (df['visitorteam_id'] == df.at[index,'visitorteam_id'])].index.item()
        except ValueError:
            print('ERROR 2 at' + str(index))
            df.at[index,'updated'] = 4

        # print('Current: ' + str(df.at[index,'fixture_id']) + '; Followed by: ' + str(next_home_fixture))
        # print('Current date rank: ' + str(df.at[index,'date']) + ' ' + str(df.at[index,'date_rank']) + '; Next home date rank: ' + str(df.at[next_home_index,'date_rank']) + '; Next away date rank: ' + str(df.at[next_away_index,'date_rank']))

        df.at[next_home_index, 'home_elo'] = update_elo(df.at[index,'home_elo'],df.at[index,'away_elo'],df.at[index,'actual_score'])
        df.at[next_away_index, 'away_elo'] = update_elo(df.at[index,'away_elo'],df.at[index,'home_elo'],1 - df.at[index,'actual_score']) # Swap function inputs for away team


        df.at[next_home_index, 'updated'] = df.at[next_home_index, 'updated'] + 1
        df.at[next_away_index, 'updated'] = df.at[next_away_index, 'updated'] + 1

        df.at[index,'updated'] = 3

該代碼適用於前幾行。 然而,我遇到錯誤,總是針對相同的行,即使我看不出這些行與其他行有何不同。

  1. 如果我不處理如上所示的ValueError ,我會收到錯誤消息ValueError: can only convert an array of size 1 to a Python scalar for the first time after about 250 rows.
  2. 如果我確實處理了如上所示的ValueError ,我會捕獲四個這樣的錯誤,每個錯誤處理塊有兩個(否則代碼工作正常),但代碼在所有行的大約 18% 之后停止更新任何進一步的強度評級,不拋出任何錯誤信息。

如果您能幫助我 (a) 了解導致錯誤的原因以及 (b) 如何處理它們,我將不勝感激。

由於這是我在 StackOverflow 上的第一篇文章,我還沒有完全了解論壇的常見發帖習慣。 如果我的帖子有什么可以改進的地方,請告訴我。

非常感謝!

供參考,

如果您將.item應用於 numpy 數組,您將收到類似的錯誤。

在這種情況下,您可以使用.tolist()解決它。

pd.Series.item需要系列中至少有一項才能返回標量。 如果:

df[(df['date_rank'] == next_home_fixture) & (df['localteam_id'] == df.at[index,'localteam_id'])]

是一個長度為 0 的系列,那么.index.item()將拋出一個 ValueError。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM