Counting Frequency of Two Words in Job Titles
Have you ever wanted to know how many times two specific words appear in a column of job titles? This article will guide you through the process of counting the frequency of two words in a column of job titles using Python. We will use the pandas library to manipulate and analyze the data, and the regular expressions module to search for the words in the job titles.
Prerequisites
To follow along with this article, you should have a basic understanding of Python and the pandas library. You should also have Python and pandas installed on your computer. If you don't have these installed, you can follow the instructions in the pandas installation guide to install them.
Setting up the Data
Let's assume we have a CSV file with a column called "Job Title" that contains the job titles we want to analyze. We can use pandas to read the CSV file and store the data in a DataFrame:
import pandas as pd
df = pd.read\_csv("job\_titles.csv")
Counting the Frequency of Two Words
To count the frequency of two words in the "Job Title" column, we can use the str.contains() method to search for the words and the value\_counts() method to count the number of occurrences:
words = ["Programme", "Manager"]
mask = df["Job Title"].str.contains("|".join(words))
count = df[mask]["Job Title"].value\_counts()
In this example, we are searching for the words "Programme" and "Manager" in the "Job Title" column. The str.contains() method returns a Boolean mask indicating whether the words are present in each job title. We then use this mask to select the rows that contain the words and count the number of occurrences using the value\_counts() method.
Displaying the Results
We can display the results using the head() method:
print(count.head())
This will print the top 5 results, showing the number of job titles that contain the two words "Programme" and "Manager" in any order.
In this article, we have shown you how to count the frequency of two words in a column of job titles using Python and pandas. This can be useful if you want to know how many times two specific words appear in a list of job titles. By using the str.contains() method to search for the words and the value\_counts() method to count the number of occurrences, you can quickly and easily analyze the data and get the information you need.
References
- pandas documentation: https://pandas.pydata.org/docs/
- regular expressions module documentation: https://docs.python.org/3/library/re.html