Search code examples
pythonpandasdataframejupyter-notebookgrouping

Grouping Count of values in Pandas dataframs


The sample data in my dataframe is as follows.

enter image description here

What I want to do is create a new dataframe from this one that contains 2 columns, I want to have all 12 community areas (as below) and the count of times "OFFENSE INVOLVING CHILDREN" from the primary type column shows up for each of these communities.

enter image description here

I've used group by and count functions and haven't been able to achieve this.

I figured out how to use the group by function but have not been able to also count the number of times that value shows up for each of the communities.


Solution

  • IIUC, value_counts gives you the count of the

    df.loc[df['Primary Type'].eq('OFFENSE INVOLVING CHILDREN')]['Community Area'].value_counts()
    

    PS: if you had shared the minimal reproducible example, it would have allowed me to share the results too