Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count frequency of values in pandas DataFrame column

I want to count number of times each values is appearing in dataframe.

Here is my dataframe - df:

    status 1     N 2     N 3     C 4     N 5     S 6     N 7     N 8     S 9     N 10    N 11    N 12    S 13    N 14    C 15    N 16    N 17    N 18    N 19    S 20    N 

I want to dictionary of counts:

ex. counts = {N: 14, C:2, S:4}

I have tried df['status']['N'] but it gives keyError and also df['status'].value_counts but no use.

like image 523
Kishan Mehta Avatar asked Mar 15 '16 07:03

Kishan Mehta


People also ask

How do you count occurrences in a column in pandas?

Using the size() or count() method with pandas. DataFrame. groupby() will generate the count of a number of occurrences of data present in a particular column of the dataframe.

How do I count the number of unique values in a column in pandas?

You can use the nunique() function to count the number of unique values in a pandas DataFrame.

How can you get the frequency of different levels in a categorical column in Python?

To create a frequency column for categorical variable in an R data frame, we can use the transform function by defining the length of categorical variable using ave function. The output will have the duplicated frequencies as one value in the categorical column is likely to be repeated.

How do you count occurrences of a string in pandas?

count() function is used to count occurrences of pattern in each string of the Series/Index. This function is used to count the number of times a particular regex pattern is repeated in each of the string elements of the Series. Valid regular expression. For compatibility with other string methods.


2 Answers

You can use value_counts and to_dict:

print df['status'].value_counts() N    14 S     4 C     2 Name: status, dtype: int64  counts = df['status'].value_counts().to_dict() print counts {'S': 4, 'C': 2, 'N': 14} 
like image 169
jezrael Avatar answered Oct 11 '22 21:10

jezrael


An alternative one liner using underdog Counter:

In [3]: from collections import Counter  In [4]: dict(Counter(df.status)) Out[4]: {'C': 2, 'N': 14, 'S': 4} 
like image 22
Colonel Beauvel Avatar answered Oct 11 '22 20:10

Colonel Beauvel