1. Bitcoin and Cryptocurrencies: Full dataset, filtering, and reproducibility
Since the launch of Bitcoin in 2008, hundreds of similar projects based on the blockchain technology have emerged. We call these cryptocurrencies (also coins or cryptos in the Internet slang). Some are extremely valuable nowadays, and others may have the potential to become extremely valuable in the future1. In fact, on the 6th of December of 2017, Bitcoin has a market capitalization above $200 billion.
*
# Importing pandas
import pandas as pd
# Importing matplotlib and setting aesthetics for plotting later.
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
plt.style.use('fivethirtyeight')
# Reading datasets/coinmarketcap_06122017.csv into pandas
dec6 = pd.read_csv('datasets/coinmarketcap_06122017.csv')
# Selecting the 'id' and the 'market_cap_usd' columns
market_cap_raw = dec6[['id','market_cap_usd']]
# Counting the number of values
# ... YOUR CODE FOR TASK 2 ...Discard the cryptocurrencies without a market capitalization
These cryptocurrencies are of little interest to us in this analysis, so they are safe to remove.
# Filtering out rows without a market capitalization
cap = market_cap_raw.query('market_cap_usd > 0')
print(cap.count())
# Counting the number of values again
# ... YOUR CODE FOR TASK 3 ...How big is Bitcoin compared with the rest of the cryptocurrencies?
Let's plot the market capitalization for the top 10 coins as a barplot to better visualize this.
#Declaring these now for later use in the plots
TOP_CAP_TITLE = 'Top 10 market capitalization'
TOP_CAP_YLABEL = '% of total cap'
# Selecting the first 10 rows and setting the index
cap10 = cap[:10].set_index('id')
# Calculating market_cap_perc
cap10 = cap10.assign(market_cap_perc = lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100)
# Plotting the barplot with the title defined above
ax = cap10.plot.bar()
plt.title("Top 10 market capitalization")
ax.set_ylabel("% of total cap")
# Annotating the y axis with the label defined above
# ... YOUR CODE FOR TASK 4 ...Making the plot more informative
While the plot above is informative enough, it can be improved. Bitcoin is too big, and the other coins are hard to distinguish because of this. Instead of the percentage, let's use a log10 scale of the "raw" capitalization. Plus, let's use color to group similar coins and make the plot more informative1.
# Colors for the bar plot
COLORS = ['orange', 'green', 'orange', 'cyan', 'cyan', 'blue', 'silver', 'orange', 'red', 'green']
# Plotting market_cap_usd as before but adding the colors and scaling the y-axis
ax = cap10['market_cap_usd'].plot.bar(title="Top 10 market capitalization",logy=True, color=COLORS)
ax.set_ylabel('USD')
# Annotating the y axis with 'USD'
# ... YOUR CODE FOR TASK 5 ...
ax.set_xlabel("")
# Final touch! Removing the xlabel as it is not very informative
# ... YOUR CODE FOR TASK 5 ...Volatility in cryptocurrencies
Let's explore this volatility a bit more! We will begin by selecting and plotting the 24 hours and 7 days percentage change, which we already have available.
# Selecting the id, percent_change_24h and percent_change_7d columns
volatility = dec6[['id', 'percent_change_24h', 'percent_change_7d']]
# Setting the index to 'id' and dropping all NaN rows
volatility = volatility.set_index('id').dropna()
# Sorting the DataFrame by percent_change_24h in ascending order
volatility = volatility.sort_values('percent_change_24h',ascending = True)
volatility.head()
# Checking the first few rows
# ... YOUR CODE FOR TASK 6 ...Well, we can already see that things are a bit crazy
It seems you can lose a lot of money quickly on cryptocurrencies. Let's plot the top 10 biggest gainers and top 10 losers in market capitalization.
#Defining a function with 2 parameters, the series to plot and the title
def top10_subplot(volatility_series, title):
# Making the subplot and the figure for two side by side plots
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 6))
# Plotting with pandas the barchart for the top 10 losers
ax = volatility_series[:10].plot.bar(color='darkred', title='top losers',ax=axes[0])
# Setting the figure's main title to the text passed as parameter
# ... YOUR CODE FOR TASK 7 ...
fig.suptitle(title)
# Setting the ylabel to '% change'
# ... YOUR CODE FOR TASK 7 ...
ax.set_ylabel('% change')
# Same as above, but for the top 10 winners
ax = volatility_series[-10:].plot.bar(color='darkblue', title='top winners',ax=axes[1])
# Returning this for good practice, might use later
return fig, ax
DTITLE = "24 hours top losers and winners"
# Calling the function above with the 24 hours period series and title DTITLE
fig, ax = top10_subplot(volatility.percent_change_24h, title= DTITLE)Let's check the weekly Series too.
# Sorting percent_change_7d in ascending order
volatility7d = volatility.sort_values("percent_change_7d")
WTITLE = "Weekly top losers and winners"
# Calling the top10_subplot function
fig, ax = top10_subplot(volatility7d.percent_change_7d, WTITLE);Exploring large cap coins