1

I am scraping this website (https://www.coingecko.com/en/coins/recently_added?page=1) using Pandas df. I want to get a snapshot of the coins and price and then get another snapshot of the coins and price 10 seconds later to see whether a coin has moved at price. Here is the code that I am using to do this:

import pandas as pd
import requests
import time

url = "https://www.coingecko.com/en/coins/recently_added?page=1"
df = pd.read_html(requests.get(url).text, flavor="bs4")
df = pd.concat(df).drop(["Unnamed: 0", "Unnamed: 1"], axis=1)
# print(df.head)
df_first = df[['Coin', 'Price']]
time.sleep(10)

url = "https://www.coingecko.com/en/coins/recently_added?page=1"
df = pd.read_html(requests.get(url).text, flavor="bs4")
df = pd.concat(df).drop(["Unnamed: 0", "Unnamed: 1"], axis=1)
df_last = df[['Coin', 'Price']]


df_merged = df_first.merge(df_last, left_on=['Coin'], right_on=['Coin'])
df_merged.set_index(['Coin'], inplace=True)
df_merged['pct_change'] = df_merged[['Price_x','Price_y']].pct_change(axis=1)['Price_y']
df_merged['greater_than_1'] = df_merged['pct_change'] > 10.00
print(df_merged)

When I run the code I am getting the error

TypeError: unsupported operand type(s) for /: 'str' and 'str'

not sure why. Any help welcome

Harry
  • 27
  • 4
  • Please post *full* stack trace of error – Gulzar May 19 '21 at 11:42
  • Does this answer your question? [Converting strings to floats in a DataFrame](https://stackoverflow.com/questions/16729483/converting-strings-to-floats-in-a-dataframe) – Gulzar May 19 '21 at 11:46

2 Answers2

1

Because the Price is a string column. You can check dtypes using the df.info() method.

You need to convert the string '$1,234.5' to float. You can do this by using the converters argument:

def parse_price(x):
    return float(x.replace('$', '').replace(',', ''))

url = "https://www.coingecko.com/en/coins/recently_added?page=1"
converters = {"Price": parse_price}

df = pd.read_html(requests.get(url).text, flavor="bs4", converters=converters)
Alexander Volkovsky
  • 2,588
  • 7
  • 13
0

You are trying to divide two strings. Obviously no language will like that!

if you converted both to a float then it'd work. See here for converting columns:

https://datatofish.com/convert-string-to-float-dataframe/

df['DataFrame Column'] = df['DataFrame Column'].astype(float)

But - looking at your problem - are you really sure this is the way you want to go about looking at price deltas? Would you not look at using one of the apis for an existing server, Binance for instance, and using their ticker streams?

Amiga500
  • 1,258
  • 1
  • 6
  • 11