-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_trends_analysis.py
More file actions
56 lines (45 loc) · 1.96 KB
/
google_trends_analysis.py
File metadata and controls
56 lines (45 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import STL
# Create a directory for analysis output
output_dir = 'analysis_output'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Load combined data
combined_data = pd.read_csv('combined_trends_data.csv', index_col=0, parse_dates=True)
# Define key global events related to climate change
global_events = {
pd.Timestamp('2009-12-01'): 'COP15 Copenhagen',
pd.Timestamp('2015-12-01'): 'Paris Agreement',
pd.Timestamp('2021-11-01'): 'COP26 Glasgow'
}
# Function to perform STL decomposition and plot
def analyze_city_data(city_data, city_name):
for keyword in city_data.columns:
if keyword == 'City':
continue
# Perform STL decomposition
stl = STL(city_data[keyword], seasonal=13)
result = stl.fit()
# Plot the results
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(city_data.index, result.trend, label='Trend', color='blue')
ax.set_title(f'STL Decomposition of {keyword} in {city_name}')
ax.set_xlabel('Year')
ax.set_ylabel('Trend')
# Annotate global events
for date, event in global_events.items():
if date in result.trend.index:
ax.annotate(event, xy=(date, result.trend.loc[date]), xytext=(date, result.trend.loc[date] + 5),
arrowprops=dict(facecolor='black', shrink=0.05), fontsize=8, rotation=45)
plt.legend()
plt.tight_layout()
# Save the plot
plt.savefig(f'{output_dir}/{city_name.lower().replace(" ", "_")}_{keyword}_trend.png')
plt.close()
# Analyze data for each city
for city in combined_data['City'].unique():
city_data = combined_data[combined_data['City'] == city].drop(columns=['City'])
analyze_city_data(city_data, city)
print("Analysis complete. Check the 'analysis_output' directory for results.")