我对 Python3 和编码很痴迷,所以请放轻松。 :)
作为一个项目,我正在根据 2018 年 EPL 成绩创建一个足球联赛表。我已经能够使用 Pandas 模块将包含整个季节数据的 .csv 文件分解为逐轮结果,再分解为 .csv。现在我需要根据每支球队的主客场进球数,逐轮提取每支球队的积分榜。我很难将每场比赛的进球与球队联系起来。我可以弄清楚如何应用赢/平/输 (3/1/0) 点,但只能在每个固定装置中手动应用,而不是对本轮中的所有固定装置动态应用。然后我需要将表写入另一个 .csv 文件。
FTHG-全场主场进球,FTAG-全场客场进球,FTR-全场赛果
- 示例数据
未命名:0,日期,主队,客队,FTHG,FTAG,FTR
2018年8月0,10日,曼联,莱斯特城,2,1,H
2018 年 8 月 1,11 日,伯恩茅斯,卡迪夫,2,0,H
2018年8月2,11日,富勒姆, Crystal 宫,0,2,A
2018/08/3,11,哈德斯菲尔德,切尔西,0,3,A
4,11/08/2018,纽卡斯尔,托特纳姆热刺,1,2,A
5,11/08/2018,沃特福德,布莱顿,2,0,H
2018/08/6,11,狼队,埃弗顿,2,2,D
7,12/08/2018,阿森纳,曼城,0,2,A
8,12/08/2018,利物浦,西汉姆联,4,0,H
2018/08/9,12,南安普顿,伯恩利,0,0,D
示例代码
import pandas as pd results = pd.read_csv("2018 Round 1.csv") team = results.iloc[2,2] if results.iloc[2,4] > results.iloc[2,5]: points = 3 elif results.iloc[2, 4] < results.iloc[2, 5]: points = 0 else: results.iloc[2, 4] = results.iloc[2, 5] points = 1 table_entry = (team + " " + str(points)) print(table_entry) table_entry = pd.to_csv("EPL Table Round 1.csv", index = False)
感谢您的帮助。
最佳答案
我希望这有帮助:) 如果代码不清楚请随时询问
import pandas as pd
import numpy as np
df = pd.read_csv('foot.txt')
#Make a list with all tema names
Home_teams = pd.unique(df['HomeTeam'])
Away_teams = pd.unique(df['AwayTeam'])
teams = np.concatenate((Home_teams, Away_teams))
df_teams = pd.DataFrame(columns=['team', 'points'])
#For each team in the list...
for team in teams:
print("*******" + team+ "*****")
points = 0
df_home = df[(df['HomeTeam'] == team)]
res_home = df_home['FTR'].value_counts()
try:
points += res_home['H']*3;
except:
print("Didn't win when Home")
try:
points += res_home['D']*1;
except:
print("No Draws")
df_away = df[(df['AwayTeam'] == team)]
res_away = df_away['FTR'].value_counts()
try:
points += res_away['A']*3;
except:
print("Didn't win when Away")
df_teams = df_teams.append({'team': team, 'points': points}, ignore_index=True)
print(team +"has "+ str(points) +" points" )
关于python - 关联 .csv 列数据以进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60204807/