计算一个团队得分的中位数?

2024-09-28 19:35:34 发布

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

Teams  Points Fouls
TeamA  12     4 
TeamA  14     6 
TeamB  25     9
TeamB  27     11
TeamB  29     13 
TeamA  16     15  
TeamA  18     7 

def pointsmedian(points):
    df = pd.DataFrame(points)
    median_points = df[(df['Teams'] == 'TeamA') & (df['Points'])].median()
    return median_points

我希望只返回A队得分的中位数,但是,我的代码也返回犯规的中位数。有什么建议吗


Tags: 代码dataframedfreturndefpointspdmedian
1条回答
网友
1楼 · 发布于 2024-09-28 19:35:34

我想你在找这个:

df.groupby('Teams')['Points'].median()

Teams
TeamA    15
TeamB    27
Name: Points, dtype: int64

如果您想仅为teamA获取,可以在groupby上使用loc

df.groupby('Teams',as_index=False)['Points'].median().set_index('Teams').loc['TeamA']

Out[397]: 
Points    15
Name: TeamA, dtype: int64

相关问题 更多 >