Getting frequency counts of a columns in Pandas DataFrame

Getting frequency counts of a columns in Pandas DataFrame

To get the frequency counts of a column in a Pandas DataFrame, you can use the value_counts() method. This method returns a Pandas Series containing counts of unique values in descending order.

Here's an example:

import pandas as pd # Sample DataFrame data = {'Category': ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'C']} df = pd.DataFrame(data) # Get frequency counts counts = df['Category'].value_counts() print(counts) 

Output:

A 3 C 3 B 2 Name: Category, dtype: int64 

If you want to see the frequencies in terms of proportions rather than counts, you can use the normalize parameter:

proportions = df['Category'].value_counts(normalize=True) print(proportions) 

Output:

A 0.375 C 0.375 B 0.250 Name: Category, dtype: float64 

The above would give you the proportion of each unique value in the specified column.


More Tags

pytest chartjs-2.6.0 javapns reduce predict dimensions jks jmx onesignal google-translation-api

More Programming Guides

Other Guides

More Programming Examples