pandas.core.groupby.GroupBy.count

Compute count of group, excluding missing values.

return

Series or DataFrame Count of values within each group.

Limitations

  • This function may reveal slower performance than Pandas* on user system. Users should exercise a tradeoff between staying in JIT-region with that function or going back to interpreter mode.

Examples

Compute count of group, excluding missing values.
import pandas as pd
import numpy as np
from numba import njit


@njit
def df_groupby_count():
    df = pd.DataFrame({'A': [1, 2, 3, 1, 2, 3, 3, 3, 2],
                       'B': [0, 1, np.nan, np.nan, 2, 4, 3, 2, np.inf],
                       'C': [np.nan, 2, 3, np.nan, 5, 6, 7, 8, 9]})
    out_df = df.groupby('A').count()

    # Expect DataFrame of
    # {'B': [1, 3, 3], 'C': [0, 3, 4} with index=[1, 2, 3]
    return out_df


print(df_groupby_count())
$ python ./dataframe/groupby/dataframe_groupby_count.py
   B  C
1  1  0
2  3  3
3  3  4

See also

Series.groupby

Group Series using a mapper or by a Series of columns.

DataFrame.groupby

Group DataFrame using a mapper or by a Series of columns.