我正在编写一些Python来绘制饼图。我试图找出使用某种阵型得分的进球数。每行的格式为:
date home_team away_team home_score away_score home_formation away_formation
14/06/2018 Russia Saudi Arabia 5 0 4-2-3-1 4-1-4-1
如果主队拥有4-2-3-1的阵型,我想说的是,将home_score添加到计数器中。最后,我将这些目标显示在饼图中
for x in df:
if df.home_formation == '4-1-4-1':
counter += df.home_score
formationScore1 = counter
我的问题:如何查看CSV文件,检查是否是我想要的格式,然后将行分数添加到变量中
答案 0 :(得分:2)
formations = ['4-1-4-1', '4-2-3-1'] #Add as many as you'd like
formation_scores = {formation: df[df['home_formation'] == formation]['home_score'].sum() for formation in formations}
答案 1 :(得分:0)
Pandas本身支持分组操作。例如,使用groupby
:
scores_by_formation = df.groupby('home_formation')['home_score'].sum()
如果只需要特定形式的分数,则可以先过滤数据框:
L = ['4-1-4-1', '4-2-3-1']
scores_by_specific_formations = df.loc[df['home_formation'].isin(L)]\
.groupby('home_formation')['home_score'].sum()
通过这些操作,您可以利用矢量化NumPy功能并避免显式的Python级循环。