Count the frequency of words in a pandas dataframe in Python
In this tutorial, we will learn how to count the frequency of words in a pandas dataframe in Python.
So here we will take a look at three ways that are there to find the frequency of words in a pandas dataframe.
First of all, we need to import the pandas library to use it:
import pandas as pd
Now let us take a dataframe we are going to use:
things = pd.DataFrame({ 'colors' : ['red', 'blue', 'yellow', 'pink', 'blue', 'black', 'white', 'red', 'pink', 'white', 'yellow', 'blue'], 'animals' : ['dog', 'cat', 'rabbit', 'cow', 'lion', 'cat', 'cow', 'lion', 'dog', 'lion', 'dog','rabbit'] })
count() function in pandas
The first way is by using the count() function:
count = things.groupby(['colors']).count() print(count)
Output:
colors black 1 blue 3 pink 2 red 2 white 2 yellow 2
size() function
The second way is by using the size function:
count = things.groupby(['colors']).size() print(count)
Output:
colors black 1 blue 3 pink 2 red 2 white 2 yellow 2
value_counts() function in pandas
The third way is by using value_counts()
function:
count = things['colors'].value_counts() print(count)
Output:
blue 3 red 2 yellow 2 pink 2 white 2 black 1 Name: colors
Also read: Mapping values in a Pandas Dataframe
Leave a Reply