gpt4 book ai didi

python - pandas 中的分组和聚合

转载 作者:行者123 更新时间:2023-12-02 01:31:10 25 4
gpt4 key购买 nike

    code_presentation   code_module score   id_student  id_assessment   date_submitted
0 2013J AAA 78.0 11391 1752 18
1 2013J AAA 70.0 11391 1800 22
2 2013J AAA 72.0 31604 1752 17
3 2013J AAA 69.0 31604 1800 26
.....

我需要计算提交的天数和如何正确分组以获得如下结果:

id_student  id_assessment date_submitted
11391 1752 1
1800 1
31604 1752 1
1800 1

...等等

我尝试:

analasys_grouped = analasys.groupby ( 'id_student', as_index = False)\
.agg({'id_assessment':'count', 'date_submitted': 'count'})
analasys_grouped

但它无法正常工作

最佳答案

如果我没理解错的话,你想申请value_counts()id_student 分组的 id_assessment。尝试:

assessment_count_per_student = df.groupby('id_student')['id_assessment'].value_counts()

print(assessment_count_per_student)

id_student id_assessment
11391 1752 1
1800 1
31604 1752 1
1800 1
Name: id_assessment, dtype: int64

关于python - pandas 中的分组和聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73246935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com