How to count the frequency of values in a Pandas DataFrame
Sometimes, when you use DataFrame
, you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one.
df.groupby().count()
Series.value_counts()
df.groupby().size()
In the following sections, we will use the same DataFrame
as follows:
import pandas as pd
df = pd.DataFrame(
{
"A": ["jim", "jim", "jim", "jim", "sal", "tom", "tom", "sal", "sal"],
"B": ["a", "b", "a", "b", "b", "b", "a", "a", "b"],
}
)
df.groupby().count()
method
This method is best if you want to count the frequency of a single column.
import pandas as pd
df = pd.DataFrame(
{
"A": ["jim", "jim", "jim", "jim", "sal", "tom", "tom", "sal", "sal"],
"B": ["a", "b", "a", "b", "b", "b", "a", "a", "b"],
}
)
freq = df.groupby(["A"]).count()
print(freq)
freq = df.groupby(["B"]).count()
print(freq)
Output:
B
A
jim 4
sal 3
tom 2
A
B
a 4
b 5
Series.value_counts()
method
Since every DataFrame
object is Series
a collection of objects, this method is best used with pandas.Series
objects.
Now use Series.values_counts()
the function
import pandas as pd
df = pd.DataFrame(
{
"A": ["jim", "jim", "jim", "jim", "sal", "tom", "tom", "sal", "sal"],
"B": ["a", "b", "a", "b", "b", "b", "a", "a", "b"],
}
)
freq = df["A"].value_counts()
print(freq)
freq = df["B"].value_counts()
print(freq)
Output:
jim 4
sal 3
tom 2
Name: A, dtype: int64
b 5
a 4
Name: B, dtype: int64
df.groupby().size()
method
The above two methods cannot be used to calculate the frequency of multiple columns, but we can use them on multiple columns at the same time df.groupby().size()
.
import pandas as pd
df = pd.DataFrame(
{
"A": ["jim", "jim", "jim", "jim", "sal", "tom", "tom", "sal", "sal"],
"B": ["a", "b", "a", "b", "b", "b", "a", "a", "b"],
}
)
freq = df.groupby(["A", "B"]).size()
print(freq)
Output:
A B
jim a 2
b 2
sal a 1
b 2
tom a 1
b 1
dtype: int64
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to set values for specific cells in a Pandas DataFrame using index
Publish Date:2025/05/02 Views:118 Category:Python
-
Pandas is a data-centric python package that makes data analysis in python easy and consistent. In this article, we will look at different ways to access and set specific cell values in a pandas DataFrame data structure using indexing
Querying a column from multiple columns based on value in Pandas
Publish Date:2025/05/01 Views:100 Category:Python
-
In this tutorial, you will learn how to perform lookup operations in Pandas. Steps to find from one of multiple columns based on value in Pandas Following are the steps to lookup from one of multiple columns based on the value in Pandas Dat
Combine values from multiple columns into one column in Pandas DataFrame
Publish Date:2025/05/01 Views:152 Category:Python
-
This tutorial will demonstrate how to merge or return the first non-null values from multiple columns into another column in a Python Pandas DataFrame. For example, if it is not empty, the value of column 1 is used for the new column
Filling Missing Values in Pandas DataFrame
Publish Date:2025/05/01 Views:127 Category:Python
-
Sometimes, we may have a dataset with missing values. There are many ways to replace the missing data using certain methods. ffill() (Forward Fill) is one of the methods to replace missing values in DataFrame. This method replaces NaN
Replace column values in Pandas DataFrame
Publish Date:2025/05/01 Views:139 Category:Python
-
In this tutorial post, we will look at how to replace column values in a Pandas DataFrame. We will look at three different functions to easily replace column values. map() Replace column values in Pandas using method The columns
How to Check if NaN Exists in a Pandas DataFrame
Publish Date:2025/04/30 Views:167 Category:Python
-
NaN Stands for Not a Number- Not a Number , which indicates missing values in Pandas. To detect NaN values in Python Pandas, we can use the isnull() and isna() methods on the DataFrame object. pandas.DataFrame.isnull() method We
Return unique values in MongoDB
Publish Date:2025/04/27 Views:191 Category:MongoDB
-
In this article, we will address how to use the MongoDB distinct() method to return unique values. In addition, returning unique values in arrays and fields is discussed. In MongoDB, sometimes you may want to present or return unique
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:67 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:186 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi