python - Pandas - 阅读 HTML

标签 python pandas

我正在尝试转换 this将表格放入 pandas DataFrame

到目前为止,我已经完成了以下工作

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 是 header 。我还希望列值 År 成为索引值。

最后,我想将 Folkmängd 列值绘制为 Y 并将 År 值绘制为 X ,在线图中。

提前谢谢你。

最佳答案

这应该接近你想要的:

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

关于python - Pandas - 阅读 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35241210/

相关文章:

Python 在字符串中查找模式

python - Pandas Dataframe 中的 MultiIndex Groupby

python - 如何将 pandas 列分配给其他列,或者如果为 nan 则为默认值?

python - 删除文本中的标记链接

Python - 使用 strptime() 将 "20:52:30.0000000+02:00"转换为日期时间

python - 在不下载网页的情况下使用 Python 检查链接是否失效

python - 使用Python从excel文件中写出没有时间的日期

python - 对列中的数据进行求和和计数

Python:删除每组中具有最大值的行

python - Azure 函数 - Python - ServiceBus 输出绑定(bind) - 设置自定义属性