Pandas read_html检索表

时间:2017-04-11 11:25:47

标签: pandas

我正在尝试从维基URL中提取奥运会奖牌表,我正在使用Python Pandas。

import pandas as pd
url = 'https://en.wikipedia.org/wiki/All-time_Olympic_Games_medal_table'
df = pd.read_html(url, skiprows=7, header = None)
df[0]

然而,我输了5行

  

名称

     

阿富汗(AFG)阿尔及利亚(ALG)

     

阿根廷(ARG)

     

亚美尼亚(ARM)

一旦我设置了skiprows = 0 - 6将返回灾难表框架,所以至少我必须将skiprows设置为6。

有没有人才推荐任何技巧来检索完美表而不是手动插入行?

由于

1 个答案:

答案 0 :(得分:2)

您可以将skiprows更改为2,然后按df[1]选择第二个表格:

url = 'https://en.wikipedia.org/wiki/All-time_Olympic_Games_medal_table'
df = pd.read_html(url, skiprows=2, header = None)
a = df[1]
print (a)
                                              0   1     2     3     4   \
0                               Afghanistan (AFG)  14     0     0     2   
1                                   Algeria (ALG)  13     5     4     8   
2                                 Argentina (ARG)  24    21    25    28   
3                                   Armenia (ARM)   6     2     5     7   
4                         Australasia (ANZ) [ANZ]   2     3     4     5   
5                       Australia (AUS) [AUS] [Z]  26   147   163   187   
6                                   Austria (AUT)  27    18    33    36   
7                                Azerbaijan (AZE)   6     7    11    25   
相关问题