You can explore how climate has changed over the years using this interactive Plotly map. By hovering over any country and moving through the timeline, you can see how average temperatures have changed across the globe. Deployed Heroku app can be found here Link to GitHub here
Import libraries first
import numpy as np
import pandas as pd
import plotly as py
import plotly.express as px
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
load the data
you can download the original data from here
df = pd.read_csv('../assets/GlobalLandTemperaturesByCountry.csv')
Look at the data, find the Null values, clean the data
print(df.shape)
df.head()
df.tail()
df.isnull().sum()
df = df.drop('AverageTemperatureUncertainty', axis=1)
df = df.rename(columns={'dt': 'Date', 'AverageTemperature': 'Average_Temp'})
df.isnull().sum()
df = df.dropna()
print(df.shape)
df.head()
Creating Celsius to Fahrenheit function
def convert_to_fahrenheit(c):
return (c*9/5)+32
Round up the result to 2 decimals as well
df['Average_Temp_Fahrenheit'] = df['Average_Temp_Celsius'].apply(convert_to_fahrenheit).round(2)
df['Average_Temp_Celsius'] = df['Average_Temp_Celsius'].round(2)
Group the dataframe by Country name and dataset
df_sorted = df.groupby(['Country', 'Date']).sum().reset_index().sort_values(['Date'], ascending=False)
df_sorted.head()
Create the data range
start_date = '2009-12-01'
end_date = '2013-01-01'
mask = (df_sorted['Date'] > start_date) & (df_sorted['Date'] <= end_date)
Create a new sorted dataset
df_2000 = df_sorted[mask]
print(df_2000.shape)
df_2000.head()
Create a dataset for Climate change by timeline
df_2000_by_month = df_2000.groupby(['Date','Country']).sum().reset_index()
df_2000_by_month.head()
Visualization
fig = px.choropleth(df_2000_by_month, locations='Country', locationmode='country names', color='Average_Temp_Fahrenheit', hover_data=['Average_Temp_Celsius'], hover_name='Country', animation_frame='Date')
fig.update_layout(title_text='Average Temperature Change from 2010-01-01 to 2013-01-01', title_x = 0.5, geo=dict(showframe = False, showcoastlines = False))
fig.show()