Overview

The interest_by_region() function in the pytrends library allows you to retrieve interest data for specific search terms across different geographic locations. By analyzing this data, you can gain valuable insights into the popularity of search terms in different regions, which can help inform your marketing and content strategies.

In this tutorial, we will cover:

  1. Importing the necessary libraries
  2. Setting up the pytrends request
  3. Retrieving interest by region data
  4. Visualizing the results

Installation

To install Pytrends, simply use pip:

pip install matplotlib

Retrieve Interest by Region Data

First, we need to import the necessary libraries and set up our pytrends request.

from pytrends.request import TrendReq
import pandas as pd

# Set up pytrends request
pytrends = TrendReq(hl='en-US', tz=360)

Next, we’ll specify the search term and time range for our request using the build_payload() function, and then retrieve interest by region data using the interest_by_region() function.

keywords = ['Python']
timeframe = '2023-01-01 2023-03-31'

# Build payload
pytrends.build_payload(keywords, timeframe=timeframe, geo='')

# Retrieve interest by region data
region_interest = pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=False)

This will return a DataFrame containing interest by region data for the search term ‘Python’ during the first quarter of 2023.

Visualizing the Results

Now, we can visualize the interest by region data using a bar plot.

import matplotlib.pyplot as plt

# Sort the data by interest value
region_interest = region_interest.sort_values(by='Python', ascending=False)

# Plot the interest by region data
plt.figure(figsize=(12, 6))
plt.bar(region_interest.index, region_interest['Python'])

plt.xlabel('Country')
plt.ylabel('Interest')
plt.title('Interest by Region for Python (Q1 2023)')
plt.xticks(rotation=90)
plt.show()

This plot shows the interest for the search term ‘Python’ across different countries, allowing you to identify areas where the term is particularly popular.

Conclusion

In this post, we’ve demonstrated how to use the interest_by_region() function in the pytrends library to analyze interest by region for specific search terms. By exploring this data, you can gain targeted insights into the popularity of search terms across different geographic locations, helping you to better understand your audience and optimize your marketing strategies. This tutorial has covered the process of collecting and analyzing interest by region data, from setting up the pytrends request to visualizing the results.


NOTE : pytrends uses an unofficial API. Please use here for issues.

SAMPLE CODE : https://github.com/hobbyworker/google-trend-for-python