函数式编程
所以我尝试了这个并且效果很好ncols=[]for i in range(len(BuffaloBillsO.columns)): ncols.append(BuffaloBillsO.columns[i][1])ncols=dict(zip(BuffaloBillsO.columns,ncols))BuffaloBillsO.columns =BuffaloBillsO.columns.to_series().map(ncols)以下是 BuffaloBillsO.columns 的输出Index(['Week', 'Day', 'Date', 'Unnamed: 3_level_1', 'Unnamed: 4_level_1', 'OT', 'Unnamed: 6_level_1', 'Opp', 'Tm', 'Opp', 'Cmp', 'Att', 'Yds', 'TD', 'Int', 'Sk', 'Yds.1', 'Y/A', 'NY/A', 'Cmp%', 'Rate', 'Att', 'Yds', 'Y/A', 'TD', 'FGM', 'FGA', 'XPM', 'XPA', 'Pnt', 'Yds', '3DConv', '3DAtt', '4DConv', '4DAtt', 'ToP'], dtype='object')
眼眸繁星
您可以通过以下方式传入标题级别.read_html():import pandas as pdurl = 'https://www.pro-football-reference.com/teams/buf/2020/gamelog/'BuffaloBillsO = pd.read_html(url,header=1)[0]输出:print(BuffaloBillsO.columns)Index(['Week', 'Day', 'Date', 'Unnamed: 3', 'Unnamed: 4', 'OT', 'Unnamed: 6', 'Opp', 'Tm', 'Opp.1', 'Cmp', 'Att', 'Yds', 'TD', 'Int', 'Sk', 'Yds.1', 'Y/A', 'NY/A', 'Cmp%', 'Rate', 'Att.1', 'Yds.2', 'Y/A.1', 'TD.1', 'FGM', 'FGA', 'XPM', 'XPA', 'Pnt', 'Yds.3', '3DConv', '3DAtt', '4DConv', '4DAtt', 'ToP'], dtype='object')