- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the frequency of a value in a DataFrame column in Pandas
To count the frequency of a value in a DataFrame column in Pandas, we can use df.groupby(column name).size() method.
Steps
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print the input DataFrame, df.
Print frequency of column, x.
Print frequency of column, y.
Print frequency of column, z.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 5], "y": [4, 10, 5, 10], "z": [1, 1, 5, 1] } ) print "Input DataFrame is:
", df col = "x" count = df.groupby('x').size() print "Frequency of values in column ", col, "is:
", count col = "y" count = df.groupby('y').size() print "Frequency of values in column ", col, "is:
", count col = "z" count = df.groupby('z').size() print "Frequency of values in column ", col, "is:
", count
Output
Input DataFrame is: x y z 0 5 4 1 1 2 10 1 2 1 5 5 3 5 10 1 Frequency of values in column x is: x 1 1 2 1 5 2 dtype: int64 Frequency of values in column y is: y 4 1 5 1 10 2 dtype: int64 Frequency of values in column z is: z 1 3 5 1 dtype: int64
- Related Articles
- How to count frequency of itemsets in Pandas DataFrame
- Python - Calculate the count of column values of a Pandas DataFrame
- How to count the NaN values in a column in a Python Pandas DataFrame?
- Python - How to Count the NaN Occurrences in a Column in Pandas Dataframe?
- Deleting a DataFrame row in Python Pandas based on column value
- Python - Calculate the variance of a column in a Pandas DataFrame
- Capitalize first letter of a column in Pandas dataframe
- Python - Calculate the standard deviation of a column in a Pandas DataFrame
- How to shift a column in a Pandas DataFrame?
- Python – Create a new column in a Pandas dataframe
- Apply uppercase to a column in Pandas dataframe
- Python - Replace values of a DataFrame with the value of another DataFrame in Pandas
- Python Pandas – Count the rows and columns in a DataFrame
- Python - Add a new column with constant value to Pandas DataFrame
- How to sort a column of a Pandas DataFrame?

Advertisements