熊猫 - 阅读HTML

时间:2016-02-06 12:50:45

标签: 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是标题。我还希望列值År是索引值。

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

提前谢谢。

1 个答案:

答案 0 :(得分:2)

这应该接近你想要的:

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