python - 在 Python Pandas 中使用 groupby 从 bool (真/假)列返回百分比结果

标签 python pandas group-by

我想返回某些小部件(红色、蓝色、绿色或紫色)的百分比(通过)率。我添加了一列(通过)来测试小部件是否在公差范围内(包括 13 到 14),现在使用 groupby 函数我想返回颜色通过的百分比,我只是不知道如何添加这个到我的 groupby 功能。我的代码是:

#Create new series called Pass to test if in tolerance
widgets['Pass'] = widgets.Vals.between(13, 14, inclusive=True)
print(widgets.head(20))

#Group by Colours - blue, green, purple, red
#Give results - count, pass(%), mean, var
widgets = widgets.groupby('Colour').Vals.agg(['count', 'mean', 'var'])
print(widgets)

结果是:

            Vals  Colour   Pass
0   13.166671     red   True
1   13.844101     red   True
2   13.667672  purple   True
3   13.457526     red   True
4   13.512347     red   True
5   13.421277   green   True
6   13.692874     red   True
7   13.768719     red   True
8   13.489158    blue   True
9   13.487612    blue   True
10  13.611823  purple   True
11  14.193408     red  False
12  13.344894  purple   True
13  13.585790  purple   True
14  13.274137    blue   True
15  13.447983     red   True
16  13.766603    blue   True
17  13.711676  purple   True
18  13.514098    blue   True
19  13.326753     red   True
        count       mean       var
Colour                            
blue    11225  13.500443  0.064099
green   11014  13.504158  0.063477
purple  11143  13.496960  0.064687
red     16618  13.501518  0.062595

最佳答案

# you can use a apply function to do your own calculations.

widgets.groupby('Colour')['Pass'].apply(lambda x: np.sum(x)/len(x))
Out[289]: 
Colour
blue      1.000000
green     1.000000
purple    1.000000
red       0.888889
Name: Pass, dtype: float64

关于python - 在 Python Pandas 中使用 groupby 从 bool (真/假)列返回百分比结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709132/

相关文章:

python - 按重量分组

python - 如何使用 pyqt4 在 TreeView 中搜索项目名称

Pandas 按总计分组

python - 值错误: cannot reindex from a duplicate axis Error in Pandas

python - 使用 Pandas Groupby 和 Apply 函数时处理 None 值

mysql - 5 个表,首先扫描以在第一个关系对或第二个关系对中查找单个匹配,无需全表扫描

python - 根据字典的其他值更改 python 字典的值

python - 流上的正则表达式而不是字符串?

python - 如何在 Tornado RequestHandler 中存储数据库 session

mysql - Group By 来查找 SQL 中表元素之间的差异?