Pandas, Get count of a single value in a Column of a Dataframe

Viewed 48102

Using pandas, I would like to get count of a specific value in a column.I know using df.somecolumn.ravel() will give me all the unique values and their count.But how to get count of some specific value.

In[5]:df
Out[5]:
        col 
         1
         1
         1
         1
         2
         2
         2
         1

Desired :

  To get count of 1.

  In[6]:df.somecalulation(1)
  Out[6]: 5

  To get count of 2.

  In[6]:df.somecalulation(2)
  Out[6]: 3
3 Answers

Get the total count:

column = df['specific_column']

column.count() 

Get the specific value total count:

column.loc[specific_column > 0].count() 

You do not need to add quotes ('') to indicate specific_column.

Related