为什么我的 Plotly Express 图形无法工作?- “ValueError:Plotly Express 无法处理具有不同类型列的宽格式数据。”

Ash*_*ssa 5 python data-visualization dataframe python-3.x plotly

所以我试图在这里做一些事情 - 我显然一点经验都没有 - 我从维基百科表格中读取数据,然后将其基本绘制在条形图上。

这非常混乱,但我本质上想做的是制作一个包含两个变量的字典,然后将其用作我的“数据帧”进行可视化。

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()
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误代码

ValueError: Plotly Express cannot process wide-form data with columns of different type.
Run Code Online (Sandbox Code Playgroud)

所以我想我理解数据类型或绘图或其他东西的方式存在一些微妙的错误(在我的脑海中,我输入的数据似乎应该很容易绘制)。

有人可以提供的任何帮助将不胜感激。

ves*_*and 4

如果没有所有导入和完整的代码片段,很难确定,但如果您的数据处理过程是健全的,那么这应该可以工作:

df = pd.DataFrame(dictionary)
fig = px.bar(df, x= 'Countries', y = 'Scores')
fig.show()
Run Code Online (Sandbox Code Playgroud)