Python for analyzing annual temperatures in Moscow

Python is a powerful programming language that is widely used in the field of data analytics, including weather data analysis. Let's use the example of analyzing annual temperatures in Moscow to figure out how it can be used to download, preprocess and visualize data for a beginner in this business.

First, you will need to access weather data. You can use APIs such as OpenWeatherMap APIto obtain weather data. OpenWeatherMap API provides access to current and historical weather data around the world.

In our example, we will do without using the API and limit ourselves to downloading an archive of weather data from the information resource https://rp5.ru/Weather_in_Moscow_(VDNH)

In our example, we will do without using API and limit ourselves to downloading the archive of weather data from the information resource https://rp5.ru/Weather_in_Moscow_(VDNH)

Download the data archive for the period of interest in the format csv or xls, save it on your PC. Then, you can use the libraries Pythonsuch as Pandas And Plotly, for data analysis and visualization. Pandas allows you to work efficiently with tabular data, and Plotly – create graphics and visualizations.

In our example we will use Pandas to read data from excel file and then use functions Pandas for filtering, grouping and aggregation of data.

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import matplotlib
import dash 
import plotly.express as px
import plotly as go 

moscow_weather = pd.read_excel('C://moscow_weather.xlsx')

moscow_weather = moscow_weather.rename(columns={'Местное время в Москве (ВДНХ)':'timestamp'})
moscow_weather.info()
First, let's load all the necessary libraries (in the first cell with the code).  Using the “pd.read_excel” function we will read the archive with weather data, do not forget to indicate the path to the file on your PC.  Let's change the column names to more convenient ones.

First, let's load all the necessary libraries (in the first cell with the code). Using the “pd.read_excel” function we will read the archive with weather data, do not forget to indicate the path to the file on your PC. Let's change the column names to more convenient ones.

At the data preprocessing stage, we will convert all “temporary” data (year, month, date, time, etc.) into timestamp format for ease of working with them in the future.

Using the “groupby” function, we group the data by year, and apply aggregation to the temperature data by minimum, maximum, sum and average.

Let's find the median values ​​of each of the resulting variables and calculate the relative deviations from the median annual temperature.

#datetime split
moscow_weather['timestamp'] = pd.to_datetime(moscow_weather['timestamp'])
moscow_weather['year'] = moscow_weather.timestamp.dt.year
moscow_weather['date'] = moscow_weather.timestamp.dt.date
moscow_weather['month'] = moscow_weather.timestamp.dt.month
moscow_weather['time'] = moscow_weather.timestamp.dt.time

annual_sample_temp = moscow_weather

#temperature analysis
annual_sample_temp = annual_sample_temp.groupby(['year'])['T'].agg(['mean','sum', 'min', 'max'])
annual_sample_temp = annual_sample_temp.reset_index()

median_min_T = annual_sample_temp['min'].median()
median_max_T = annual_sample_temp['max'].median()
median_avg_T = annual_sample_temp['sum'].median()
median_avg_T = annual_sample_temp['mean'].median()

annual_sample_temp['rel_mean'] = (annual_sample_temp['mean']-median_avg_T)/annual_sample_temp['mean']

From the resulting dataset, we will plot deviations from the median temperature value on the graph. We will use the Plotly library to create it.

import plotly.graph_objects as go

# Добавляем данные в график
year = annual_sample_temp['year'].to_list()
high = annual_sample_temp['max'].to_list()
low = annual_sample_temp['min'].to_list()
median = annual_sample_temp['mean'].to_list()


fig = go.Figure()
# Создаем графики динамики температуры
fig.add_trace(go.Scatter(x=year, y=high, name="Max",
                         line=dict(color="firebrick", width=4)))
fig.add_trace(go.Scatter(x=year, y=low, name="Min",
                         line=dict(color="royalblue", width=4)))
fig.add_trace(go.Scatter(x=year, y=median, name="Average",
                         line=dict(color="grey", width=4,
                              dash="dash") # dash options include 'dash', 'dot', and 'dashdot'
))

# Редактировать макет
fig.update_layout(title="Средние, высокие и низкие годовые температуры в Москве",
                   xaxis_title="Год",
                   yaxis_title="Температура (градусы по Цельсию)",
                   hovermode="x unified")

fig.show()

In this simple way, we prepared and visualized weather data for Moscow for further analysis and search for deviations using a programming language Python. We have an interactive dashboard that can be saved as a separate image; you can leave the visualization of only the minimum, maximum or average temperature using the interactive legend buttons in the upper right corner.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *