0

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)

here is the output of info(): enter image description here

Roxana Slj
  • 311
  • 1
  • 5
  • 23
  • What error do you get? – Simone Dec 03 '21 at 12:07
  • Could not convert string to float, I also have added image to my question – Roxana Slj Dec 03 '21 at 12:19
  • 1
    Does this answer your question? [Convert number strings with commas in pandas DataFrame to float](https://stackoverflow.com/questions/22137723/convert-number-strings-with-commas-in-pandas-dataframe-to-float) – Sam Mason Dec 03 '21 at 12:41
  • No as I mentioned in my question and you can see in comment, I have tried that and still getting same error – Roxana Slj Dec 03 '21 at 13:08
  • I have deleted my answer since I missunderstood your question and clearly didn't responded to the question. So, just to clarify. You get a list of values and in this list you have: value of MarketCap, value of Volume, value of Open, value of Close, right? And you want to create a dictionary for each `coin` and add to it those values in `float` format – Alexandru DuDu Dec 03 '21 at 13:54
  • What do you think `'ethereum'` as a `float` would look like? Because that is what `'Volume_'+str(float(str(coin).replace(",",".")))` is attempting to do. – Axe319 Dec 03 '21 at 14:01
  • Yes, and since all of these happening in the loop so I can dynamically implement it for all 3 (coins)/ tables – Roxana Slj Dec 03 '21 at 14:04
  • for all 3 currencies (URL). The cell values should be floating point numbers.execpt date which is datetime. this is what I am trying to do – Roxana Slj Dec 03 '21 at 14:05
  • I think you have a misunderstanding of basic python. The line `dict_columns_type = `, etc. has nothing to do with `pandas`. It is a pure python operation creating a `dict` mapping a string to a type. You start out correctly with `'Market Cap_'+str(coin): float`. The rest isn't even valid syntax. – Axe319 Dec 03 '21 at 14:06
  • Ok, could you suggest any other approach? – Roxana Slj Dec 03 '21 at 14:07

0 Answers0