Counting Frequency of Values in Dataframe Columns
Given a dataframe with a column containing categorical values, you may encounter the need to count the frequency of occurrence of each unique value.
Consider the following dataframe:
category cat a cat b cat a
To retrieve the distinct values and their corresponding frequencies, follow these steps:
Using value_counts()
As suggested by @DSM, utilize value_counts() to accomplish this task:
In [37]: df = pd.DataFrame({'a':list('abssbab')}) df['a'].value_counts()
Output:
b 3 a 2 s 2 dtype: int64
Using groupby() and count()
Alternatively, you can employ groupby() and count():
In [38]: df.groupby('a').count()
Output:
a a a 2 b 3 s 2 [3 rows x 1 columns]
Additional Options:
For further insight, refer to the pandas documentation at https://pandas.pydata.org.
Incorporating Frequency Back into the Dataframe
If you wish to add the frequency values back to the original dataframe, you can utilize transform() with count():
In [41]: df['freq'] = df.groupby('a')['a'].transform('count') df
Output:
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]
The above is the detailed content of How Can I Count the Frequency of Values in a Pandas DataFrame Column?. For more information, please follow other related articles on the PHP Chinese website!