我有以下来自excel文件的简化数据框
Team match1 game12 match3
1 Sandhausen 2 3 1
2 Pohlheim 1 1 6
3 Völklingen 4 2 4
4 Nieder-Olm/Wörrstadt 5 7 2
5 Nümbrecht 7 6 3
6 Dorheim 3 4 7
7 Nienburg/Weser 6 5 5
8 Bad Homburg 8 8 8
9 Bad Homburg 9 9 9
我想计算出最好的球队总数。 比赛的数据代表了球队的位置。 计算出最佳团队的1。排名第二,得9分。获得8分,以此类推。 这适用于所有比赛
我的问题是match1可能是一个完全不同的名称。是否可以使用索引
更新我使用两个答案:
要创建这样的内容:
count_row = df.shape[0]
df["score"] = (count_row+1 - df.drop(columns='Team')).sum(axis=1)
df['extra_points'] = (df ==1).sum(axis=1)
df['total'] = df.loc[:,['score','extra_points']].sum(axis=1)
df_total = df.groupby("Team").agg({"total": "sum"}).reset_index().sort_values(by='total', ascending=False)
print(df)
print(df_total)
更新了,以计算每列的最佳团队:
如果在} 和^{} :
Team
列中有重复的团队,并且您想要求和我将使用^{检查最佳团队
您也可以这样做:
基本上在这里,我选择了所有应该考虑得分的列(在本例中,除了列
Team
)[df.drop(columns=["Team"])
]然后,我将等级转换为分数(等级1->;10-1=9,等级2->;10-2=8,…,等级9->;10-9=1)[
( 10 - ... )
]在此之后,我对行(axis=1)上的所有值求和,并将其分配给列
score
[df["score"] = (...).sum(axis=1)
]其结果如下:
此外,如果您更喜欢选择要使用的列,而不是拖放,则可以使用以下方法:
筛选正在
col != "Team"
中进行,但您可以更改它相关问题 更多 >
编程相关推荐