我正在尝试解析位于 here 的表。使用下面的代码,但它返回多级索引。
url1='aboveurl.htm'
df1=pd.read_html(url1)
df1=df1[0]
最佳答案
df=df[0]
df.columns=df.columns.droplevel()
这将根据您的喜好删除多重索引。
print(df.head())
Rk School Conf W L Pct W L Pct Off \
0 1 Clemson ACC (Atlantic) 12 2 .857 7 1 .875 33.3
1 2 North Carolina State ACC (Atlantic) 9 4 .692 6 2 .750 32.2
2 3 Louisville ACC (Atlantic) 8 5 .615 4 4 .500 38.1
3 4 Wake Forest ACC (Atlantic) 8 5 .615 4 4 .500 35.3
4 5 Boston College ACC (Atlantic) 7 6 .538 4 4 .500 25.7
Def SRS SOS AP Pre AP High AP Rank Notes
0 13.6 20.62 6.84 5 1 4 NaN
1 25.2 12.17 5.55 NaN 14 23 NaN
2 27.4 9.67 3.75 16 14 NaN NaN
3 28.3 11.42 6.03 NaN NaN NaN NaN
4 22.8 9.39 7.08 NaN NaN NaN NaN
关于python - 如何使用 Pandas html 函数解析表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375375/