In this post, we will see how to get frequency counts of a column in Pandas DataFrame.
Sometimes, you might have to find counts of each unique value for the categorical column. You can use value_count() to get frequency counts easily. value_count() returns series object with frequency counts data for a column.
Here is sample Employee data that will be used in below example:
[table id=29 /]
import pandas as pd
emp_df = pd.DataFrame({'Name': ['Adam','Reena','Mahesh','Supriya','Aman'],
'Age': [27,22,33,26,34],
'Department':['HR','Sales','Tech','HR','Tech']})
print("-------Original Dataframe-------\n",emp_df)
# Get unique values using unique() method
deptCounts = emp_df.Department.value_counts()
print("-------Frequency counts for Department column-------\n",deptCounts)
print(type(deptCounts))
Output:
-------Original Dataframe-------
Name Age Department
0 Adam 27 HR
1 Reena 22 Sales
2 Mahesh 33 Tech
3 Supriya 26 HR
4 Aman 34 Tech
-------Frequency counts for Department column-------
HR 2
Tech 2
Sales 1
Name: Department, dtype: int64
As you can see, we got Frequency counts for each unique value in column Department.
That’s all about how to get frequency counts of a column in Pandas DataFrame.