Pandas:计算特定列

2024-10-03 02:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下来自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)


Tags: 数据dfcountextrateampointsrowtotal
2条回答

更新了以计算每列的最佳团队:

df.set_index('Team').idxmax()
match1    BadHomburg
game12    BadHomburg
match3    BadHomburg
dtype: object

如果在Team列中有重复的团队,并且您想要求和我将使用^{}^{}

df_ranking = ( df.melt('Team')
                 .groupby('Team')['value']
                 .sum()
                 .sort_values(ascending = False)
                 .to_frame('Points')
                 .reset_index() )

df_ranking.index = df_ranking.index + 1

print(df_ranking)
                   Team  Points
1            BadHomburg    42.0
2             Nümbrecht    16.0
3        Nienburg/Weser    16.0
4  Nieder-Olm/Wörrstadt    14.0
5               Dorheim    14.0
6            Völklingen    10.0
7              Pohlheim     8.0
8            Sandhausen     6.0

检查最佳团队

df_ranking.loc[1,'Team']
#'BadHomburg'

您也可以这样做:

df = pd.DataFrame([
        ['Sandhausen',2,3,1],
        ['Pohlheim',1,1,6],
        ['Völklingen',4,2,4],
        ['Nieder-Olm/Wörrstadt',5,7,2],
        ['Nümbrecht',7,6,3],
        ['Dorheim',3,4,7],
        ['Nienburg/Weser',6,5,5],
        ['Bad Homburg',8,8,8],
        ['Bad Homburg',9,9,9]
    ],
    columns=["Team", "match1", "game12", "match2"])

df["score"] = ( 10 - df.drop(columns=["Team"]) ).sum(axis=1)

基本上在这里,我选择了所有应该考虑得分的列(在本例中,除了列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)]

其结果如下:

                   Team  match1  game12  match2  score
0            Sandhausen       2       3       1     24
1              Pohlheim       1       1       6     22
2            Völklingen       4       2       4     20
3  Nieder-Olm/Wörrstadt       5       7       2     16
4             Nümbrecht       7       6       3     14
5               Dorheim       3       4       7     16
6        Nienburg/Weser       6       5       5     14
7           Bad Homburg       8       8       8      6
8           Bad Homburg       9       9       9      3

此外,如果您更喜欢选择要使用的列,而不是拖放,则可以使用以下方法:

df[[ col for col in df.columns if col != "Team" ]]

筛选正在col != "Team"中进行,但您可以更改它

相关问题 更多 >