Counting the frequency of words in a pandas dataframe

Given a pandas dataframe, we have to count the Frequency of words in a pandas dataframe.
By Pranit Sharma Last updated : September 03, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose, we are given a DataFrame with all the columns of string type, we need to count the frequency of each column.

Count the frequency of words in a pandas dataframe

For this purpose, you can use .split(expand=True).stack().value_counts() with the specified column whose word frequency is to be counted. The statement expand=True expands out the split elements into separate columns and .stack().value_counts() returns a Series with the values as the index and the counts as the values.

Let us understand with the help of an example,

Python program to count the frequency of words in a pandas dataframe

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d = {
    'code':['A24','B13','M88','F90'],
    'address':['Gokuldham society','db society','db colony','vasant nagar']
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display Original DataFrame
print("Created DataFrame:\n",df,"\n")

# Calculating the frequency of each word
res = df.address.str.split(expand=True).stack().value_counts()

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Counting the frequency of words in a dataframe

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.