简体   繁体   中英

Iterating through rows in a dataframe

I have a dataframe of 12 different teams with their own statistics. My objective is to repeat an entire series of steps for one team, and so on, until the last team has been processed. My code currently correctly calculates statistics for only the first row of the dataframe. I want to repeat these lines of code for each row of the dataframe. I figured that a for loop would be the way to do so, but I'm struggling with the arguments to pass through. Any help would be appreciated, thank you.

import pandas as pd 
stats = pd.read_csv('question2_data .csv')
print(stats) 
team_count = 0

Output:

  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)

Your code calculates the statistics for the first row because you're using stats.iloc[0]. So just replace the 0 for your iterator in the for loop:

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

You can use stats.shape(0) to get the number of rows.

Bonus: There's a pd.DataFrame.apply() function if you want to get each statistic in a new column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM