通过/失败数据帧示例

2024-04-28 23:45:37 发布

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

我有以下代码:

  import pandas as pd
    
    status = ['Pass','Fail']
    item_info = pd.DataFrame({
        'student': ['John','Alice','Pete','Mike','John','Alice','Joseph'],
        'test': ['Pass','Pass','Pass','Pass','Pass','Pass','Pass']
    })
    
    item_status = pd.crosstab(item_info['student'],item_info['test'])
    print(item_status)

产生:

| Student | Pass |
|---------|------|
| Alice   | 2    |
| John    | 2    |
| Joseph  | 1    |
| Mike    | 1    |
| Pete    | 1    |

但是,我想创建如下内容:

| Student | Pass | Fail | Total |
|---------|------|------|-------|
| Alice   | 2    | 0    | 2     |
| John    | 2    | 0    | 2     |
| Joseph  | 1    | 0    | 1     |
| Mike    | 1    | 0    | 1     |
| Pete    | 1    | 0    | 1     |

如何更改代码,使其包含所有学生的0失败列,并提供总数


Tags: 代码testinfostatuspassitemjohnstudent
1条回答
网友
1楼 · 发布于 2024-04-28 23:45:37

通用解决方案,在不预先知道现有标签的情况下添加额外标签,使用reindex

cols = item_info['test'].unique().tolist()+['Fail'] #adding the extra label
pd.crosstab(item_info['student'],item_info['test']).reindex(columns=cols,fill_value=0)

或者,根据您的需要,我假设您正在寻找链方法:

item_status = pd.crosstab(item_info['student'],item_info['test'])
item_status['Fail'] = 0

test     Pass  Fail
student            
Alice       2     0
John        2     0
Joseph      1     0
Mike        1     0
Pete        1     0

相关问题 更多 >