Learn Bitcoin-Python #2 - Bitcoin Price API & data visualisation

learn_bitcoin-python.png

Hi Guys,

This tutorial is in continuation of the "Learn Bitcoin through programming with Python " series.

Please visit Part 1 below:
Utopian - https://utopian.io/utopian-io/@abhi3700/learn-bitcoin-python-1-installation-guide-basic-commands
Steemit - https://steemit.com/utopian-io/@abhi3700/learn-bitcoin-python-1-installation-guide-basic-commands

Bitcoin Price API

Here, we fetch the Bitcoin-price using an API provided by Coindesk.
Tools required - Requests library. For more, click here.

Install from the 'cmd' using pip install requests -
requests_install.png

Let's get started with the coding part-
Open notebook.

import requests
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
print(r.json())

request_json.png
This displays the complete json data.

The complete json file looks like this. Click here -
view_price_json.png

Now, to get the price, we need to fetch the 'rate' parameter inside 'USD'. Follow the steps -

  • Fetch the 'bpi' data -
print(r.json()['bpi'])

price_json_bpi.png

  • Fetch the 'USD' data -
print(r.json()['bpi']['USD'])

price_bpi_usd.png

  • Fetch the 'rate' value -
print("The price of Bitcoin is: " + r.json()['bpi']['USD']['rate'])

bitcoin_api_price.png

Now, let's jump on to the next section which is in more details i.e. Price data visualisation.

Bitcoin data visualisation

Here, we will look at the graph of the Bitcoin Price from day 1 to current date. For this, we will fetch the data from Coindesk in csv format.

Tools Installation

Following tools required for this:

  • Pandas- for reading the csv file data. pip install pandas
    install_pandas.png

  • Matplotlib- plot the data. pip install matplotlib
    install_matplotlib.png

  • Jupyter notebook - writing commands for data visualisation. pip install jupyter
    install_jupyter.png

Download the csv file.

Click on the 'Export' button on right side of the graph. And 'save as' in 'csv' format in the directory of the notebook editor file.

Coding

Now, open the notebook and start writing commands as follows:

  • Import the pandas library
import pandas as pd

import_pandas.png

  • Import the matplotlib library
import matplotlib.pyplot as plt

import_matplotlib.png

  • Plot the graph inline. Also, we need to to make the changes to the data onto the original set, not onto the copy.
# The changed happen to the original dataset, not to the copy
pd.set_option('mode.chained_assignment', None)
# plot the charts within the notebook
%matplotlib inline

plot_inline.png

  • Data manipulation
# Reading the csv file using pandas
price = pd.read_csv("coindesk_data.csv")
# look into the first 5 rows of the data.
price.head()

price_head.png

Now, let's get some info about the data.

price.info()

price_info.png

Here, we have 2 columns i.e. 'Date' - 2713 entries and 'Close Price' - 2711 entries. Now, we have to look at the 'Date' entries which has 2 extra entries. Also, the 'Date' entries is of type 'object' which needs to be changed into date-format.

Now, look at the data from bottom.

price.tail()

price_tail.png
We find here that the last 2 rows have Close Price value as 'NaN', which needs to be deleted.
Use price = price.dropna() method for this.
Just check that the data is removed.
price_tail.png
So, the last 2 rows have been removed.
Now, we need to convert the Date entries into date-format.

price['Date'] = pd.to_datetime(price['Date'], format = '%Y-%m-%d')

Now, to check if implemented. Use this method price.info()
price_datetime_format.png
Here, we can see that the Date column has entries of type 'datetime'.
Now, we have to set the Date column as index column.

price.index = price['Date']

price_index.png
Here, we see that the previous Date column is also present.

So, we need to delete that using del price['Date']. Now look at the output.
del_price.png

Now, we can see the price of the year 2010 using price['2010']
price_2010.png

If we want to see the price on a particular date, use this method price['2017-12-18']
price_date.png

Now, to get the price from a date till current date, use this price['2017-08-01':]
price_august onwards.png

Now, to see the min. and max. price, use these commands - price.min() & price.max() .
price_min_max.png

To know everything at a glance, use this syntax - price.describe()
price_describe.png

Now, plot the graph of Price vs Date . Use price.plot()
price_plot.png

Now, to plot the graph for the year 2017, use price['2017'].plot()
price2017_plot.png

That's all for now...

Stay tuned for more such tutorials.

Follow the series in Github



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center