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:
Name | Age | Department |
---|---|---|
Adam | 27 | HR |
Reena | 22 | Sales |
Mahesh | 33 | Tech |
Supriya | 26 | HR |
Aman | 34 | Tech |
1 2 3 4 5 6 7 8 9 10 11 12 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
-------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 <class 'pandas.core.series.Series'> |
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.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.