![](/img/trans.png)
[英]How to output html table with merged cells from pandas DataFrame
[英]How to read html table in pandas and output to dataframe not list
我正在将html表从html文件读入pandas,并希望将其作为数据框而不是列表来获取,以便执行一般的数据框操作。
每当我尝试除打印整个数据框以外的任何操作时,我都会遇到如下错误。
print(dfdefault.shape())
AttributeError: 'list' object has no attribute 'shape'
Pandas .read_html()
函数将返回数据.read_html()
列表,其中每个数据框都是在页面上找到的表。 使用StackOverflow的联赛,我们可以看到页面右侧有两个表格。 如您在下面看到的,列表是read_html()
返回的内容。
url = 'https://stackexchange.com/leagues/1/alltime/stackoverflow'
df_list = pd.read_html(url)
print(df_list)
# [ Rep Change* Users <-- first table
# 0 10,000+ 15477
# 1 5,000+ 33541
# 2 2,500+ 68129
# 3 1,000+ 155430
# 4 500+ 272683
# 5 250+ 429742
# 6 100+ 458600
# 7 50+ 458600
# 8 1+ 458600,
# Total Rep* Users <-- second table
# 0 100,000+ 697
# 1 50,000+ 1963
# 2 25,000+ 5082
# 3 10,000+ 15477
# 4 5,000+ 33541
# 5 3,000+ 56962
# 6 2,000+ 84551
# 7 1,000+ 155430
# 8 500+ 272683
# 9 200+ 458600
# 10 1+ 10381503]
print(len(df_list))
# 2
在这里,您只需要指定要使用的表即可。 如果只有一张桌子,很容易确定要使用哪一张桌子。
df = df_list[0]
print(df)
# Rep Change* Users
# 0 10,000+ 15477
# 1 5,000+ 33541
# 2 2,500+ 68129
# 3 1,000+ 155430
# 4 500+ 272683
# 5 250+ 429742
# 6 100+ 458600
# 7 50+ 458600
# 8 1+ 458600
print(df.shape)
# (9, 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.