为什么我的绘声绘色的数字不起作用?“ValueError:Plotly Express无法处理具有不同类型列的宽格式数据。”

2024-09-30 20:22:56 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在这里尝试做一些事情——我显然一点经验都没有——我从维基百科的表格中读取数据,然后在条形图上初步绘制出来

这是相当混乱的,但我基本上要做的是制作一个包含这两个变量的字典,然后将其用作可视化的“数据框架”

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()

我一直收到这个错误代码

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

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

如果有人能提供任何帮助,我将不胜感激


Tags: infordatatablebodyallfindrows
1条回答
网友
1楼 · 发布于 2024-09-30 20:22:56

如果没有所有的导入和完整的代码段,很难确定,但是如果数据处理过程是合理的,那么这应该是可行的:

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

相关问题 更多 >