#!/usr/bin/env python # coding: utf-8 # ## Storytelling Data Visualization on Exchange Rates # # ### Introduction # # This dataset describes Euro daily exchange rates between 1999 and 2021. The euro (symbolized with €) is the official currency in most of the countries of the European Union. If the exchange rate of the euro to the US dollar is 1.5, you get 1.5 US dollars if you pay 1.0 euro (one euro has more value than one US dollar at this exchange rate). Daria Chemkaeva put together the data set and made it available on [Kaggle](https://www.kaggle.com/lsind18/euro-exchange-daily-rates-19992020)— the data source is the European Central Bank. Note that the dataset gets regular updates — we downloaded it on January 2021. # # The aim of this project is to show the effect euro-dollar rate during the coronavirus pandemic, by using the 2020 data and the 2016-2019 data as a baseline. # ### Introducing the Dataset # In[1]: #Import pandas and read in the data import pandas as pd exchange_rates = pd.read_csv('euro-daily-hist_1999_2020.csv') # In[2]: #display first five rows exchange_rates.head(5) # In[3]: #display last five rows exchange_rates.tail(5) # In[4]: exchange_rates.info() # In this dataset, there are a total of 41 columns and 5699 rows. Some of the rows have null values with a mixture of float and object data types. # ### Data Cleaning # In[5]: #rename two columns in the dataframe exchange_rates.rename({'Period\\Unit:':'Time', '[US dollar ]':'US_dollar'}, axis=1, inplace=True) #change the Time column to a datetime data type exchange_rates['Time'] = pd.to_datetime(exchange_rates['Time']) #sort the values by Time column in ascending order exchange_rates.sort_values('Time', inplace=True) #reset the index and drop the initial index exchange_rates.reset_index(drop=True, inplace=True) # In[6]: euro_to_dollar = exchange_rates[['Time','US_dollar']] euro_to_dollar # In[7]: #check the counts of unique values in 'US_dollar' column euro_to_dollar['US_dollar'].value_counts() # In[8]: #Drop all the rows where the '-' character appears in the US_dollar column euro_to_dollar = euro_to_dollar[euro_to_dollar.US_dollar != '-'] #convert US_dollar column to a float data type euro_to_dollar['US_dollar'] = euro_to_dollar['US_dollar'].astype(float) euro_to_dollar.info() # ### Rolling Mean # In[9]: #generate a line plot to visualize the evolution of the euro-dollar exchange rate import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar']) plt.show() # In[10]: plt.figure(figsize=(15,12)) plt.subplot(3, 2, 1) plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar']) plt.title('Original values', weight='bold') rolling = [7, 30, 50, 100, 365] for i, rolls in zip(range(2,7), rolling): plt.subplot(3, 2, i) plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar'].rolling(rolls).mean()) plt.title('Rolling Window:' + str(rolls), weight='bold') plt.show() # In[11]: euro_to_dollar['rolling_mean'] = euro_to_dollar['US_dollar'].rolling(30).mean() # ### Effect of Coronavirus pandemic on Euro-Dollar rate # We will show how the euro-dollar rate has changed during the coronavirus pandemic and show the 2020 data and the 2016-2019 data as a baseline. # In[12]: coronadata_2020 = euro_to_dollar.copy()[euro_to_dollar['Time'].dt.year == 2020] before_corona = euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2016) & (euro_to_dollar['Time'].dt.year <= 2019)] # In[13]: ### Adding the FiveThirtyEight style import matplotlib.style as style style.use('fivethirtyeight') ### Adding the subplots plt.figure(figsize=(11, 6)) ax1 = plt.subplot(2,2,1) ax2 = plt.subplot(2,2,2) ax3 = plt.subplot(2,1,2) axes = [ax1, ax2, ax3] ### Changes to all the subplots for ax in axes: ax.set_ylim(0.8, 1.6) ax.set_yticks([1.0, 1.2, 1.4]) ax.set_yticklabels(['1.0', '1.2','1.4'], alpha=0.3) ax.grid(alpha=0.5) ### Ax1: Before corona 2016-2019 ax1.plot(before_corona['Time'], before_corona['rolling_mean'], color='#0055ff') ax1.set_xticklabels(['', '2016', '', '2017', '', '2018', '', '2019'], alpha=0.3) ax1.text(16750, 1.8,'Pre-Coronavirus (2016-2019)', fontsize=18, weight='bold', color='#0055ff') ### Ax2: corona 2020 ax2.plot(coronadata_2020['Time'], coronadata_2020['rolling_mean'], color='#a00a4d') ax2.set_xticklabels(['Q1-2020', '', 'Q2-2020', '', 'Q3-2020', '', 'Q4-2020'], alpha=0.3) ax2.text(18300, 1.8, 'Coronavirus (2020)', fontsize=18, weight='bold', color='#a00a4d') ### Ax3: Merge both times ax3.plot(before_corona['Time'], before_corona['rolling_mean'], color='#0055ff') ax3.plot(coronadata_2020['Time'], coronadata_2020['rolling_mean'], color='#a00a4d') ax3.grid(alpha=0.5) ax3.set_xticks([]) ### Adding a title and a subtitle ax1.text(16350, 2.10, 'EURO-USD rate peaked at 1.24 and 1.21 before and after the Coronavirus Pandemic', fontsize=20, weight='bold') ax1.text(16350, 2.00, '''EURO-USD exchange rates in Pre-Coronavirus Era (2016-2018) and Coronavirus Era (2020)''' ,fontsize=16) ### Adding a signature ax3.text(16500, 0.65, '©DATAQUEST' + ' '*140 + 'Source: European Central Bank', color = '#f0f0f0', backgroundcolor = '#4d4d4d', size=14) plt.show() # ### Conclusion # # We built three graphs, two on the top row and one graph on the bottom row (where the two grids are merged). We did this to see how the effect the pandemic on the exchange rates from 2016 to 2020. From the plot, we can see that the exchange rate fluctuates,there was a mild rate decline in 2016 but it rises steadily mid 2017, where the rate is at an all time high but dips in 2018 and 2019. The decline is uniform until the end of q1 2020 where we see a steady movement upwards even till the end of Q4 2020. Overall, the exchange rates increased mostly in 2020, which may likely be due to the pandemic.