Why won't my Plotly express figure work? - "ValueError: Plotly Express cannot process wide-form data with columns of different type."

Viewed 5381

So I'm trying to do something here - I'm obviously not very experienced at all - where I'm reading in data from a wikipedia table and then rudimentally plotting it on a bar chart.

It's pretty messy but what I'm essentially trying to do is make a dictionary containing the two variables and then use that as my "DataFrame" for the visualisation.

source = requests.get('https://en.wikipedia.org/wiki/List_of_countries_by_alcohol_consumption_per_capita').text
soup  = BeautifulSoup(source, 'lxml')
body = soup.find('body')
table = body.find('table')
tablemain = table.find('table', class_ ='wikitable nowrap sortable mw-datatable')

countrylist = []
for data in tablemain.find_all('tbody'):
rows = data.find_all('tr')
rows.pop(0)
for row in rows:
    country = row.find('a')
    countrylist.append(country)

countrynames = []
for x in countrylist:
names = x.get('title')
countrynames.append(names)

total = []
for data in tablemain.find_all('tbody'):
rows = data.find_all('tr')
rows.pop(0)
for row in rows:
    tot = row.find_all('td') [1]
    total.append(tot.text)

floatal = []
for i in total:
i = float(i)
floatal.append(i)

countries = tuple(countrynames)
dictionary = {'Countries':countrynames,'Scores':floatal}

fig = px.bar(dictionary)
fig.show()

I keep getting this error code

ValueError: Plotly Express cannot process wide-form data with columns of different type.

So I imagine there's something subtly wrong with the way I am understanding data types or plotly or something (in my head the data that im feeding in seems like it should be easily plotable).

Any help someone could give would be really appreciated.

1 Answers

Hard to know for sure without all your imports and a complete code snippet, but if your data handling process is sound, then this should work:

df = pd.DataFrame(dictionary)
fig = px.bar(df, x= 'Countries', y = 'Scores')
fig.show()
Related