【发布时间】:2021-11-18 12:35:37
【问题描述】:
我有结果、学生、版本和状态列。在这个我想通过使用 Student , Version 和 result = pass count 和 result = fail count 来分组
类似于 df.groupby(["student", "version", "result=pass"]).size().reset_index(name="new_result")
下面是我的数据框
| result | student | version | status | Failed Subject |
|---|---|---|---|---|
| pass | Student-A | L-1.0 | Active | |
| fail | Student-A | L-1.0 | Active | Mathematics |
| fail | Student-A | L-1.0 | Active | Physics |
| pass | Student-A | M-1.0 | Active | |
| fail | Student-A | M-1.0 | Active | Science |
| pass | Student-B | N-1.0 | Active | |
| pass | Student-B | N-1.0 | Active | |
| pass | Student-B | N-1.0 | Active | |
| pass | Student-C | O-1.0 | Active | |
| pass | Student-C | O-1.0 | Active | |
| fail | Student-C | O-1.0 | Active | English |
| fail | Student-C | P-1.0 | Active | Computers |
| fail | Student-C | P-1.0 | Active | Mathematics |
我希望我的输出数据框如下:
| student | version | pass_count | fail_count | status | total_count (pass+fail) | Failed Subject |
|---|---|---|---|---|---|---|
| Student-A | L-1.0 | 1 | 2 | Active | 3 | Mathematics,Physics |
| Student-A | M-1.0 | 1 | 1 | Active | 2 | Science |
| Student-B | N-1.0 | 3 | 0 | Active | 3 | |
| Student-C | O-1.0 | 1 | 1 | Active | 2 | English |
| Student-C | P-1.0 | 0 | 2 | Active | 2 | Computers,Mathematics |
我可以使用以下但不是总计数来获得通过和失败计数,请任何人帮助
pd.pivot_table(master_df, index=['status', 'student', 'version'], columns=['result'], aggfunc=len, fill_value=0)
【问题讨论】: