Python KeyError列名不在索引中

2024-10-16 20:52:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我又问了一个新手问题。从Excel获得以下数据框,我发现错误,是否有人能告诉我我做错了什么,谢谢:

编辑: 以下是Excel中的列

+----+--------+------+----+----+---+----+-----+----+-----+----+-------+-------+----------+--------+-----------+--------+-----+--------+------------+------+----------+-------+------------+-------+---------+-----+-----+-----+-----+------+----+-----+------+-----------+----------+----------+---------+
| Rk | Player | From | To | Tm | G | GS | Yrs | PB | AP1 | AV | GS/Yr | AV/Yr | QB_score | Height | Height-in | Weight | BMI | School | BCS School | 40YD | Vertical | Bench | Broad Jump | 3cone | Shuttle | Cmp | Att | Pct | Yds | AY/A | TD | Int | Rate | Rush_Att  | Rush_Yds | Rush_Avg | Rush_TD |
+----+--------+------+----+----+---+----+-----+----+-----+----+-------+-------+----------+--------+-----------+--------+-----+--------+------------+------+----------+-------+------------+-------+---------+-----+-----+-----+-----+------+----+-----+------+-----------+----------+----------+---------+

KeyError: "['Rush_Att'] not in index"

df = pd.read_excel('sportsref-qbdata-raw-v2.xlsx', header=1)

cols_to_keep = ['Player', 'QB_score', 'Height-in', 'Weight', 'BMI', 'BCS School', 'Cmp', 'Att', 'Pct', 'Yds', 'AY/A', 'TD', 'Int', 'Rate', 'Rush_Att', 'Rush_Yds', 'Rush_Avg', 'Rush_TD']

df = df[cols_to_keep] 


Tags: ingsdfexcelatttdscoreplayer