如何统计CSV数据中的团队总数?修复TypeError: object of type 'int' has no len()错误
如何统计数据中的团队总数?
我正在尝试统计数据里的团队总数,写了如下Python代码:
import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/avinashjairam/avinashjairam.github.io/master/question2_data%20.csv', header=None) for index, rows in df.iterrows(): team_info = rows.to_list() team_id = team_info[0] wins = team_info[1] losses = team_info[2] ties = team_info[3] print(team_id, wins, losses, ties) print(f'Team {team_id} has {wins} wins, {losses} losses, and {ties} ties.') total_games_played = wins + losses + ties print(f'Total Games Played: {total_games_played}') games_remaining = 16 - total_games_played if games_remaining == 0: print('The season is finished.') else: pass print(f'Total Games Remaining: {games_remaining}') winning_average = (wins/total_games_played) if total_games_played != 0 else 0 print(f'The Winning Average is: {winning_average:.4f}') if (ties >= wins): print('The number of tied games are greater than or equal to the number of wins.') else: print('The number of tied games are not greater than or equal to the number of wins.') if ties >= losses: print('The number of tied games are greater than the number of losses.') else: print('The number of tied games are not greater than the number of losses.') wip_total = (wins + ties) - (3 * losses) if wip_total < 0: print(f'The Wip Total is: 0.') else: print(f'The Wip Total is: {wip_total}') Total_Number_of_Teams = len(int(team_id)) print(f'The Total Number of Teams are {Total_Number_of_Teams}.')
但运行时收到了如下错误:
TypeError Traceback (most recent call last) <ipython-input-101-f8b0a4f5d307> in <module> ----> 1 Total_Number_of_Teams = len(int(team_id)) 2 print(f'The Total Number of Teams are {Total_Number_of_Teams}.') TypeError: object of type 'int' has no len()
我试过用len()和count()函数,但还是遇到同样的错误,请问该怎么正确统计team_id里的团队总数?
错误原因分析
你现在的问题出在这行代码:
Total_Number_of_Teams = len(int(team_id))
team_id是每一行里的单个团队ID,把它转成整数后再用len()肯定会报错——因为整数类型没有长度属性,len()是用来统计序列(比如列表、字符串)的元素个数的,不是用来处理单个数值的。而且你把这段代码放在for循环里,每次循环都只处理一行数据,这样根本没法统计整个数据集里的团队总数。
解决方案
其实统计团队总数非常简单,你不需要在循环里处理,直接利用DataFrame的特性就能实现:
方法1:直接获取DataFrame的行数
因为每一行对应一个团队,所以团队总数就是整个DataFrame的行数,你可以用len(df)或者df.shape[0]来获取:
把原来循环里的那两行统计团队总数的代码删掉,然后在循环外面(比如读取完数据之后)加上:
total_teams = len(df) # 或者 total_teams = df.shape[0] print(f'The Total Number of Teams are {total_teams}.')
方法2:统计不重复的团队ID(如果有重复行的情况)
如果你的数据里可能存在重复的团队ID(同一团队有多行数据),那你需要统计去重后的数量:
total_unique_teams = df[0].nunique() print(f'The Total Number of Unique Teams are {total_unique_teams}.')
修改后的完整代码示例
import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/avinashjairam/avinashjairam.github.io/master/question2_data%20.csv', header=None) # 先统计团队总数,放在循环外面 total_teams = len(df) print(f'The Total Number of Teams are {total_teams}.\n') for index, rows in df.iterrows(): team_info = rows.to_list() team_id = team_info[0] wins = team_info[1] losses = team_info[2] ties = team_info[3] print(team_id, wins, losses, ties) print(f'Team {team_id} has {wins} wins, {losses} losses, and {ties} ties.') total_games_played = wins + losses + ties print(f'Total Games Played: {total_games_played}') games_remaining = 16 - total_games_played if games_remaining == 0: print('The season is finished.') else: pass print(f'Total Games Remaining: {games_remaining}') winning_average = (wins/total_games_played) if total_games_played != 0 else 0 print(f'The Winning Average is: {winning_average:.4f}') if (ties >= wins): print('The number of tied games are greater than or equal to the number of wins.') else: print('The number of tied games are not greater than or equal to the number of wins.') if ties >= losses: print('The number of tied games are greater than the number of losses.') else: print('The number of tied games are not greater than the number of losses.') wip_total = (wins + ties) - (3 * losses) if wip_total < 0: print(f'The Wip Total is: 0.') else: print(f'The Wip Total is: {wip_total}') print('---') # 分隔每个团队的信息,更清晰
这样修改后,就能正确统计出团队总数,而且不会再出现类型错误啦。
内容的提问来源于stack exchange,提问作者Shan456
相关产品推荐
相关产品推荐

