简体   繁体   English

Pandas DataFrame Groupby:如何统计满足条件的分组行数

[英]Pandas DataFrame Groupby: How to count the number of grouped rows meeting a condition

I want to group rows by 'Age', and return a count of 1) how many rows make up each group, and 2) how many of those rows meet a condition.我想按“年龄”对行进行分组,并返回一个计数:1)每个组有多少行,2)这些行中有多少符合条件。

Given a DataFrame that looks like this:给定一个看起来像这样的 DataFrame:

    Age     Died
0   26      0
1   26      0
2   27      1
3   28      0
4   28      1
5   28      1

I want to return a DataFrame that looks like this:我想返回一个看起来像这样的 DataFrame:

   Age     Count    Died_Count
   26        2        0
   27        1        1
   28        3        2

I have tried numerous combinations of various groupbys such as groupby(['Age', 'Died']) with different aggregators ( sum , count ) but can't seem to find a winning combination.我尝试了各种groupbys的多种组合,例如groupby(['Age', 'Died'])与不同的聚合器( sumcount ),但似乎无法找到一个成功的组合。 Can someone point me in the right direction?有人可以指出我正确的方向吗?

You can use namedagg:您可以使用namedagg:

(
    df.groupby('Age')
    .agg(Count=('Died', 'size'),
        Died_count=('Died', 'sum'))
    .reset_index()
)

Assume your dataframe is df假设您的 dataframe 是df

res=df.groupby("Age").agg({'Age': 'count', 'Died': 'sum'}).rename(columns={"Age":"Count"})

output output

        Count  Died
Age             
26       2     0
27       1     1
28       3     2

you can reset index and set Age to a column as well.您也可以重置索引并将 Age 设置为列。

res = res.reset_index(drop=False)

output output

   Age  Count  Died
0   26      2     0
1   27      1     1
2   28      3     2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM