ValueError: could not convert string to float using Pandas

  floating-point, pandas, python, string, type-conversion

Here I would like to convert all column values to floating point numbers, I have tried the code below but getting error, I have tried and removed ‘,’ (as you can see in comments) but still getting same error.

 url_list = ['https://www.coingecko.com/en/coins/ethereum/historical_data/usd?start_date=2021-08-06&end_date=2021-09-05#panel',
            'https://www.coingecko.com/en/coins/cardano/historical_data/usd?start_date=2021-08-06&end_date=2021-09-05#panel',
           'https://www.coingecko.com/en/coins/chainlink/historical_data/usd?start_date=2021-08-06&end_date=2021-09-05#panel']


dfList = []

for url in url_list:
    response = requests.get(url)
    src = response.content
    soup = BeautifulSoup(response.text , 'html.parser')

data = []
coin = url.split("/")[5].upper()
for row in soup.select('tbody tr'):
    
    data.append(
        dict(zip([f'{x.text}_{coin}' for x in soup.select('thead th')], [x.text.strip() for x in row.select('th,td') ]))
    )
    
    
    df = pd.DataFrame(data)
    df['Date_'+str(coin)] = pd.to_datetime(df['Date_'+str(coin)])
    dict_columns_type = {'Market Cap_'+str(coin): float, 
                         'Volume_'+str(coin):float,
                         'Open_' +str(coin):float,
                         'Close_'+str(coin):float }
#         df['Market Cap_'+str(coin)] = df['Market Cap_'+str(coin)].str.replace(',', '' ).astype(float)
   
        df = df.astype(dict_columns_type)


    dfList.append(df)

Source: Python Questions

LEAVE A COMMENT