In [24]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
Analysis on how tempreature was changing in each season.
In [47]:
city_temp = pd.read_csv('data/GlobalLandTemperaturesByMajorCity.csv')
city_temp.head(5)
Out[47]:
In [48]:
city_temp = city_temp[['dt','City','AverageTemperature']]
city_temp['dt'] = pd.to_datetime(city_temp['dt'])
city_temp['year'] = city_temp['dt'].map(lambda x:x.year)
city_temp['month'] = city_temp['dt'].map(lambda x:x.month)
city_temp = city_temp[['year','month','City','AverageTemperature']]
def season_(month):
if month >=3 and month <=5:
return 'spring'
elif month >=6 and month <=8:
return 'summer'
elif month >=9 and month <=11:
return 'antumn'
else:
return 'winter'
city_temp['season'] = city_temp['month'].apply(season_)
city_temp.head(5)
Out[48]:
In [70]:
min_year = city_temp['year'].min()
max_year = city_temp['year'].max()
years = range(min_year, max_year + 1)
spring_temps = []
summer_temps = []
autumn_temps = []
winter_temps = []
for year in years:
curr_years_data = city_temp[city_temp['year'] == year]
spring_temps.append(curr_years_data[curr_years_data['season'] == 'spring']['AverageTemperature'].mean())
summer_temps.append(curr_years_data[curr_years_data['season'] == 'summer']['AverageTemperature'].mean())
autumn_temps.append(curr_years_data[curr_years_data['season'] == 'autumn']['AverageTemperature'].mean())
winter_temps.append(curr_years_data[curr_years_data['season'] == 'winter']['AverageTemperature'].mean())
sns.set(style="whitegrid")
sns.set_color_codes("deep")
f, ax = plt.subplots(figsize=(10, 6))
plt.plot(years, summer_temps, label='Summer average temperature', color='b')
plt.plot(years, autumn_temps, label='Autumn average temperature', color='r')
plt.plot(years, spring_temps, label='Spring average temperature', color='g')
plt.plot(years, winter_temps, label='Winter average temperature', color='orange')
plt.xlim(min_year, max_year)
ax.set_ylabel('Average temperature')
ax.set_xlabel('Year')
ax.set_title('Average temperature in each season')
legend = plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon=True, borderpad=1, borderaxespad=1)
In [ ]: