Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the frequency that a value occurs in a dataframe column

I have a dataset

category cat a cat b cat a 

I'd like to be able to return something like (showing unique values and frequency)

category   freq  cat a       2 cat b       1 
like image 863
yoshiserry Avatar asked Mar 13 '14 21:03

yoshiserry


People also ask

How do you count frequency in a Dataframe in Python?

To count the frequency of a value in a DataFrame column in Pandas, we can use df. groupby(column name). size() method.

How do I count the number of times a value appears in a column pandas?

We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.

How do I count a specific value in a column in pandas?

Use Sum Function to Count Specific Values in a Column in a Dataframe. We can use the sum() function on a specified column to count values equal to a set condition, in this case we use == to get just rows equal to our specific data point. If we wanted to count specific values that match another boolean operation we can.

How do you count unique values in a column in Python?

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


1 Answers

Use groupby and count:

In [37]: df = pd.DataFrame({'a':list('abssbab')}) df.groupby('a').count()  Out[37]:     a a    a  2 b  3 s  2  [3 rows x 1 columns] 

See the online docs: https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html

Also value_counts() as @DSM has commented, many ways to skin a cat here

In [38]: df['a'].value_counts()  Out[38]:  b    3 a    2 s    2 dtype: int64 

If you wanted to add frequency back to the original dataframe use transform to return an aligned index:

In [41]: df['freq'] = df.groupby('a')['a'].transform('count') df  Out[41]:     a freq 0  a    2 1  b    3 2  s    2 3  s    2 4  b    3 5  a    2 6  b    3  [7 rows x 2 columns] 
like image 76
EdChum Avatar answered Oct 03 '22 04:10

EdChum