How to get Bitcoin price using Python

1 min

Here is the simplest way I found to retrieve Bitcoin price using Python and the pandas library.

Installing pandas

pip install pandas
python 2.7
pip3 install pandas
python 3.x

Getting Bitcoin prices

# We import the Pandas library
import pandas as pd

# We perform an API call against the coingecko api
# and get back BTC coin prices.
btc_prices = pd.read_json("https://api.coingecko.com/"\
                          "api/v3/coins/bitcoin/ohlc"\
                          "?vs_currency=usd&days=30")

# We rename the columns to human readable colnames
btc_prices.columns = ["date","open", 
                      "high", "low", 
                      "close"]

# We format the date from UNIX to a Human Readable Format
btc_prices["date"] = pd.to_datetime(btc_prices["date"],
                                    unit="ms")

# We set the date column as index
btc_prices.set_index("date", inplace=True)

# We plot the close price with labels and title
btc_prices.close.plot(figsize=(5,3),
                 title="Bitcoin prices last 30 days",
                 xlabel="Date",
                 ylabel="in USD", 
                 grid=True)
How to get Bitcoin prices.

As we can see the Bitcoin prices are saved under the btc_prices variable.

Here you are ! you now know how to get Bitcoin prices using the pandas library.