[英]Iterating through rows in a dataframe
我有一個包含 12 個不同團隊的數據框,它們有自己的統計數據。 我的目標是為一個團隊重復一系列步驟,依此類推,直到最后一個團隊處理完畢。 我的代碼目前僅正確計算數據框第一行的統計信息。 我想為數據幀的每一行重復這些代碼行。 我認為 for 循環將是這樣做的方法,但我正在努力解決要通過的論點。 任何幫助將不勝感激,謝謝。
import pandas as pd
stats = pd.read_csv('question2_data .csv')
print(stats)
team_count = 0
輸出:
Team ID Wins Losses Ties
0 9867 4 2 3
1 1234 7 5 2
2 6213 9 7 0
3 1231 12 2 2
4 8821 2 7 7
5 1131 8 0 8
6 7761 10 3 3
7 6831 0 16 0
8 3131 16 0 0
9 3131 0 0 16
10 8424 0 0 0
11 4211 4 4 4
team_id = stats.iloc[0]['Team ID']
win_count = stats.iloc[0]['Wins']
loss_count = stats.iloc[0]['Losses']
tie_count = stats.iloc[0]['Ties']
print('Team', team_id)
print(win_count, 'Wins', loss_count, 'Losses', tie_count, 'Ties')
game_count = win_count + loss_count + tie_count
remaining_games_count = 16 - game_count
if (game_count == 16):
print('Games played:', game_count, 'The teams season is finished')
elif (game_count < 16):
print('Games played:', game_count, 'Games remaining:', remaining_games_count)
win_avg = round((win_count/game_count), 4)
print('Winning average:', win_avg)
if (tie_count >= win_count):
print('Games tied are greater than or equal to games won')
else:
print('Games tied are not greater than or equal to games won')
if (tie_count > loss_count):
print('Games tied are greater than games lost')
else:
print('Games tied are not greater than games lost')
wip_tot = win_count + tie_count - (loss_count*3)
if (wip_tot%2==0):
wip_tot = 0
print('WIP total:', wip_tot)
您的代碼計算第一行的統計信息,因為您使用的是 stats.iloc[0]。 因此,只需在 for 循環中替換 0 為您的迭代器:
for i in range(12):
team_id = stats.iloc[i]['Team ID']
win_count = stats.iloc[i]['Wins']
loss_count = stats.iloc[i]['Losses']
tie_count = stats.iloc[i]['Ties']
etc...
您可以使用stats.shape(0)
來獲取行數。
獎勵:如果您想在新列中獲取每個統計信息,可以使用pd.DataFrame.apply()
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.