How to count rows not values in python pandas?
Question: I would like to group DataFrame by some field like

    
    
    student_data.groupby(['passed'])
    

and then count number of rows inside each group.

I know how to count values like

    
    
    student_data.groupby(['passed'])['passed'].count()
    

or

    
    
    student_data.groupby(['passed']).agg({'passed': 'count'})
    

but this will [exclude empties by default](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.DataFrame.count.html). I would like to count all
rows in groups?

I found I can count rows in entire DataFrame with

    
    
    len(student_data.index)
    

but can't find any `index` field in `GroupBy` object or something.


Answer: You need [`value_counts`](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.Series.value_counts.html) with parameter
`dropna=False`:

    
    
    import pandas as pd
    import numpy as np
    
    student_data = pd.DataFrame({'passed':[1,1,2,2,2,np.nan,np.nan]})
    print(student_data)
       passed
    0     1.0
    1     1.0
    2     2.0
    3     2.0
    4     2.0
    5     NaN
    6     NaN
    
    print (student_data['passed'].value_counts(dropna=False))
     2.0    3
     1.0    2
    NaN     2
    Name: passed, dtype: int64
    

