0% found this document useful (0 votes)
3 views2 pages

Q6 Coding

The document provides Python code to import and structure Google Trends data for keywords related to inflation, stock market, and unemployment. It uses the pytrends library to fetch interest over time data from 2023 to 2025, processes the data, and visualizes it using matplotlib. Finally, the structured data is saved to a CSV file.

Uploaded by

kefyalewgeme
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Q6 Coding

The document provides Python code to import and structure Google Trends data for keywords related to inflation, stock market, and unemployment. It uses the pytrends library to fetch interest over time data from 2023 to 2025, processes the data, and visualizes it using matplotlib. Finally, the structured data is saved to a CSV file.

Uploaded by

kefyalewgeme
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

5.

Python code to import and structure into useful data structures


!pip install -q pytrends pandas matplotlib

from [Link] import TrendReq


import pandas as pd
import [Link] as plt

# FIX: disable retries to avoid urllib3 error


pytrends = TrendReq(
hl='en-US',
tz=360,
retries=0
)

keywords = ["inflation", "stock market", "unemployment"]

pytrends.build_payload(
kw_list=keywords,
timeframe="2023-01-01 2025-01-01",
geo=""
)

df = pytrends.interest_over_time()

if [Link]:
raise ValueError("No data returned from Google Trends")

if "isPartial" in [Link]:
[Link](columns=["isPartial"], inplace=True)

df.reset_index(inplace=True)
display([Link]())

[Link]()
for k in keywords:
[Link](df["date"], df[k], label=k)

[Link]("Google Trends Search Volume Index (2023–2025)")


[Link]("Date")
[Link]("SVI (0–100)")
[Link]()
[Link](rotation=45)
plt.tight_layout()
[Link]()
df.to_csv("/content/Google_Trends_SVI_2023_2025.csv", index=False)
print("Saved: /content/Google_Trends_SVI_2023_2025.csv")

You might also like