#!/usr/bin/env python # coding: utf-8 # # Daily Exchange Rates per Euro # # # Our focus in the guided part of the project will be on the exchange rate between the euro and the American dollar. # # 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. # In[138]: import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt import datetime as dt # Enables Jupyter to display graphs get_ipython().run_line_magic('matplotlib', 'inline') exchange_rates = pd.read_csv('euro-daily-hist_1999_2020.csv') # In[139]: #learn some basic facts about the dataset: exchange_rates.info() # In[140]: exchange_rates.head() # In[141]: exchange_rates.tail() # __Basic Facts__ # # - Rows 5699 # - Col 41 # - Null values: 0 # - Data types: object & float # __Data Cleaning__ # In[142]: #We rename the [US dollar ] and Period\Unit: columns to something easier to type — US_dollar and Time. exchange_rates.rename(columns={'[US dollar ]': 'US_dollar','Period\\Unit:': 'Time'},inplace=True) #We change the Time column to a datetime data type. exchange_rates['Time'] = pd.to_datetime(exchange_rates['Time']) #We sort the values by Time in ascending order. exchange_rates.sort_values('Time', inplace=True) #We reset the index (and drop the initial index). exchange_rates.reset_index(drop=True, inplace=True) # In[143]: #Isolate the Time and the US_dollar columns. Assign them to a different variable named euro_to_dollar. euro_to_dollar = exchange_rates[['Time','US_dollar']] #Run the Series.value_counts() method on the US_dollar column, and see if you notice anything wrong. euro_to_dollar['US_dollar'].value_counts() #Drop all the rows where the - character appears in the US_dollar column(# - 62) euro_to_dollar = euro_to_dollar[euro_to_dollar['US_dollar'] != '-'] #Convert the US_dollar column to a float data type. euro_to_dollar["US_dollar"] = euro_to_dollar["US_dollar"].astype(float) euro_to_dollar.head(20) # # __Generated line plot to visualize the evolution of the euro-dollar exchange rate.__ # In[144]: plt.style.use('default') plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar']) plt.show() # __Rolling mean__ # In[145]: #Calculate the rolling means for the US_dollar(30 days) column using a moving window of 30 days. Add the rolling means to a new column named rolling_mean. #plt.style.use('default') euro_to_dollar['rolling_mean'] = euro_to_dollar['US_dollar'].rolling(2).mean() # In[146]: #NOTE: FOR LOOP SYNTAX IS MORE CONSISE TO IMPLEMENT GRID plt.figure(figsize=(10, 12))#controls size of grid #1st subplot plt.subplot(3, 2, 1) plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar']) plt.title('Orginal value',fontweight="bold") #2nd plt.subplot(3, 2, 2) plt.plot(euro_to_dollar['Time'],euro_to_dollar['US_dollar'].rolling(7).mean()) plt.title('Rolling window 2 days',fontweight="bold") #3rd plt.subplot(3, 2, 3) plt.plot(euro_to_dollar['Time'],euro_to_dollar['US_dollar'].rolling(30).mean()) plt.title('rolling window 7 days',fontweight="bold") #4rd plt.subplot(3, 2, 4) plt.plot(euro_to_dollar['Time'],euro_to_dollar['US_dollar'].rolling(50).mean()) plt.title('rolling window 50 days',fontweight="bold") #5th plt.subplot(3, 2,5) plt.plot(euro_to_dollar['Time'],euro_to_dollar['US_dollar'].rolling(100).mean()) plt.title('rolling window 100 days', fontweight="bold") #6th plt.subplot(3, 2,6) plt.plot(euro_to_dollar['Time'],euro_to_dollar['US_dollar'].rolling(365).mean()) plt.title('rolling mean 365 days',fontweight="bold") plt.show() # __Story telling__ # # We show how the euro-dollar rate changed during the 2007-2008 financial crisis. We can also show the data for 2006 and 2009 for comparison. We can use a line plot. # __Project: Euro-USD rate peaked at 1.59 during 2007-2008 financial crisis.__ # In[147]: #get dates for usd exchange rates 2006-2010 exchange_rate_06_10 = euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2006) & (euro_to_dollar['Time'].dt.year <= 2009)] #get dates for usd exchange rates 2007-2009 financial_07_08 = euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year > 2007) & (euro_to_dollar['Time'].dt.year < 2009)] #financial_07_08.describe() #exchange_rate_06_10['Time'] # In[148]: euro_to_dollar.columns # In[151]: #line graph to show changes import matplotlib.style as style #style.available - check available styles for graph style.use('fivethirtyeight')#style of graph #adding the graph fig, ax = plt.subplots(figsize=(12, 8))#controls size of figure #2006 - 10 finanacial rate ax.plot(exchange_rate_06_10['Time'], exchange_rate_06_10['rolling_mean'],color = 'g') #2007- 8 financial crisis ax.plot(financial_07_08['Time'], financial_07_08['rolling_mean'],color = 'red') #add title and suptitle plt.suptitle('Euro-USD rate peaked at 1.59 during 2007-2008\'s financial crisis', fontweight = 'bold',fontsize = 22) plt.title('Euro-USD rate peaked between 2006 - 2010',y = 1 ,x = 0.245) #highlight peek exchange rate with vertical line #ax.axvspan([exchange_rate_06_10(2007), exchange_rate_06_10(2007)],facecolor='#2ca02c') #modify xticks and yticks #add signature #ax.text(-0.1, -1, #'©DATAQUEST Source: P. Cortez et al.', # color = '#f0f0f0', # backgroundcolor = '#4d4d4d', #size=12) plt.show() # In[ ]: