【发布时间】:2021-10-30 09:20:24
【问题描述】:
我有结果、学生、版本和状态列。在这个我想通过使用 Student , Version 和 result = pass count 和 result = fail count 来分组
类似于 df.groupby(["student", "version", "result=pass"]).size().reset_index(name="new_result")
下面是我的数据框
| result | student | version | status |
|---|---|---|---|
| pass | Student-A | L-1.0 | Active |
| fail | Student-A | L-1.0 | Active |
| fail | Student-A | L-1.0 | Active |
| pass | Student-A | M-1.0 | Active |
| fail | Student-A | M-1.0 | Active |
| 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 |
| fail | Student-C | P-1.0 | Active |
| fail | Student-C | P-1.0 | Active |
我希望我的输出数据框如下:
| student | version | pass_count | fail_count | status | total_count (pass+fail) |
|---|---|---|---|---|---|
| Student-A | L-1.0 | 1 | 2 | Active | 3 |
| Student-A | M-1.0 | 1 | 1 | Active | 2 |
| Student-B | N-1.0 | 3 | 0 | Active | 3 |
| Student-C | O-1.0 | 1 | 1 | Active | 2 |
| Student-C | P-1.0 | 0 | 2 | Active | 2 |
我可以使用以下但不是总计数来获得通过和失败计数,请任何人帮助
pd.pivot_table(master_df, index=['status', 'student', 'version'], columns=['result'], aggfunc=len, fill_value=0)
【问题讨论】:
-
尝试使用 df.pivot_table 来做上述情况。
-
@Roxy,我对 pivot_table 不熟悉,请您帮忙提供一些参考代码或者可能是确切的代码
-
我可以使用以下但不是总计数来获得通过和失败计数,任何人请帮助 pd.pivot_table(master_df, index=['status', 'student', 'version'], columns=['result'], aggfunc=len, fill_value=0)
-
您需要提供示例数据,以便我们重新创建问题。
-
查看下面的代码以获取总数