import pandas as pd
import matplotlib.pyplot as plt
import folium
df = pd.read_csv('weather_data.csv')
df_clean = df[df.STATION == "USC00111577"].reset_index()
plt.figure(figsize=(14, 6))
plt.plot(df_clean.index, df_clean['TMAX'], 'b-', linewidth=1.0, alpha=0.8, label='Daily Max Temperature (°F)')
plt.axhline(y=90, color='red', linestyle='--', linewidth=2, alpha=0.8, label='Heatwave Threshold (90°F)')
plt.xlabel('Days from 2023.9.1')
plt.ylabel('Temperature (°F)')
plt.title('Maximum Temperature (TMAX) with Heatwave Threshold in Chicago')
plt.legend()
plt.grid(True, alpha=0.3)
def plot_chicago():
chicago_center = [41.8781, -87.6298]
m = folium.Map(location=chicago_center, zoom_start=10, tiles='OpenStreetMap')
folium.Marker(
chicago_center,
popup='Chicago Downtown',
tooltip='Click for more info',
icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)
return m
plot_chicago()
df_01 = pd.read_csv('Tmax01.csv', na_values = ["NaN"])
df_01_clean = df_01.dropna()
df_01_clean.reset_index()
df_01_clean.NAME.unique()
df_01_s = df_01_clean.sort_values(["STATION", "DATE"]).reset_index(drop=True)
def count_hot_days(sub_df):
temps = sub_df["TMAX"]
mask = temps > 90
groups = (mask != mask.shift()).cumsum()
streak_lengths = mask.groupby(groups).sum()
valid_groups = streak_lengths[streak_lengths >= 2].index
total_days = mask.groupby(groups).sum().loc[valid_groups].sum()
return total_days
result_01 = df_01_s.groupby("STATION").apply(count_hot_days).reset_index(name="NumDays")
print(result_01)
df_13 = pd.read_csv('Tmax13.csv', na_values = ["NaN"])
df_13_clean = df_13.dropna()
df_13_clean.reset_index()
df_13_clean.NAME.unique()
df_13_s = df_13_clean.sort_values(["STATION", "DATE"]).reset_index(drop=True)
result_13 = df_13_s.groupby("STATION").apply(count_hot_days).reset_index(name="NumDays")
print(result_13)
df_35 = pd.read_csv('Tmax35.csv', na_values = ["NaN"])
df_35_clean = df_35.dropna()
df_35_clean.reset_index()
df_35_clean.NAME.unique()
df_35_s = df_35_clean.sort_values(["STATION", "DATE"]).reset_index(drop=True)
result_35 = df_35_s.groupby("STATION").apply(count_hot_days).reset_index(name="NumDays")
print(result_35)
combined = pd.concat([result_01, result_13, result_35])
combined = combined[~combined["STATION"].str.contains("110")]
final = combined.groupby("STATION", as_index=False)["NumDays"].sum()
print(final)
coords = df[["STATION", "LATITUDE", "LONGITUDE"]].drop_duplicates().reset_index()
final_coords = coords[coords["STATION"].isin(final["STATION"])].reset_index()
final_coords
final_with_coords = final.merge(final_coords[["STATION", "LATITUDE", "LONGITUDE"]], on="STATION", how="left")
final_with_coords["heatwave_index"] = final_with_coords["NumDays"] / 1826
final_with_coords
import folium
def plot_chicago(final_with_coords):
chicago_center = [41.8781, -87.6298]
m = folium.Map(location=chicago_center, zoom_start=10, tiles='OpenStreetMap')
folium.Marker(
chicago_center,
popup='Chicago Downtown',
tooltip='Click for more info',
icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)
for _, row in final_with_coords.iterrows():
folium.CircleMarker(
location=[row["LATITUDE"], row["LONGITUDE"]],
radius=8,
color='blue',
fill=True,
fill_color='orange',
fill_opacity=0.7,
popup=(
f"<b>Station:</b> {row['STATION']}<br>"
f"<b>Heatwave Index:</b> {row['heatwave_index']:.4f}<br>"
f"<b>NumDays:</b> {row['NumDays']}"
),
tooltip=f"{row['STATION']}"
).add_to(m)
return m
m = plot_chicago(final_with_coords)
m