Pandas阅读HTML

2024-09-23 00:26:12 发布

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

我正试图将this表转换为pandasDataFrame

到目前为止我已经做了以下工作

import pandas as pd

url = 'http://www.scb.se/sv_/Hitta-statistik/Statistik-efter-amne/Befolkning/Befolkningens-sammansattning/Befolkningsstatistik/25788/25795/Helarsstatistik---Riket/26046/'

df = pd.read_html(url,thousands=' ')
df2= df[0]

我的问题是pandas无法识别索引值0是头。我还希望列值År是索引值。

最后,我想把Folkmängd列的值绘制成Y,把År列的值绘制成X

提前谢谢你。


Tags: importhttpurlpandasdfaswww绘制
1条回答
网友
1楼 · 发布于 2024-09-23 00:26:12

这应该接近你想要的:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')

url = 'http://www.scb.se/sv_/Hitta-statistik/Statistik-efter-amne/Befolkning/Befolkningens-sammansattning/Befolkningsstatistik/25788/25795/Helarsstatistik---Riket/26046/'

table = pd.read_html(url,thousands=' ', header=0, index_col=0)[0]
table["Folkmängd"].plot(color='k')
plt.show()

它应该会给你这样的东西:

enter image description here

相关问题 更多 >