user10468005
user10468005

Reputation: 167

ValueError: could not convert string to float: 'False' in python

I am trying to scrape table data from web page by using below code but getting error:

ValueError: could not convert string to float: 'False' in this line data = (tabulate(df[0], headers='keys', tablefmt='psql') )

import pandas as pd
import requests
from bs4 import BeautifulSoup
from tabulate import tabulate

res = requests.get("http://rerait.telangana.gov.in/PrintPreview/PrintPreview/UHJvamVjdElEPTQmRGl2aXNpb249MSZVc2VySUQ9MjAyODcmUm9sZUlEPTEmQXBwSUQ9NSZBY3Rpb249U0VBUkNIJkNoYXJhY3RlckQ9MjImRXh0QXBwSUQ9")
soup = BeautifulSoup(res.content,'html.parser')

table_data = []

for i in range(len(soup.find_all('table'))):

    table = soup.find_all('table')[i] 
    df = pd.read_html(str(table))
    data = (tabulate(df[0], headers='keys', tablefmt='psql') )
    print (data)

df_1 = pd.DataFrame(data)
df_1.to_csv('D:/out_table.csv')

Error:

Traceback (most recent call last):

  File "<ipython-input-128-30edd695db38>", line 15, in <module>
    data = (tabulate(df[0], headers='keys', tablefmt='psql') )

  File "D:\Conda\lib\site-packages\tabulate.py", line 1286, in tabulate
    for c, ct, fl_fmt, miss_v in zip(cols, coltypes, float_formats, missing_vals)]

  File "D:\Conda\lib\site-packages\tabulate.py", line 1286, in <listcomp>
    for c, ct, fl_fmt, miss_v in zip(cols, coltypes, float_formats, missing_vals)]

  File "D:\Conda\lib\site-packages\tabulate.py", line 1285, in <listcomp>
    cols = [[_format(v, ct, fl_fmt, miss_v, has_invisible) for v in c]

  File "D:\Conda\lib\site-packages\tabulate.py", line 754, in _format
    return format(float(val), floatfmt)

ValueError: could not convert string to float: 'False'

Upvotes: 2

Views: 3377

Answers (1)

jpp
jpp

Reputation: 164823

The error is self-explanatory. You can't convert the string 'False' to float. What you can do is force your dataframe to numeric via pd.to_numeric, replacing non-convertible values with NaN, which is float:

dfs = pd.read_html(str(table))
dfs[0] = dfs[0].iloc[:].apply(pd.to_numeric, errors='coerce')
data = tabulate(dfs[0], headers='keys', tablefmt='psql')

Upvotes: 2

Related Questions