比较 df.head 和 df.columns 中的列时没有匹配项
因此,我正在使用 https://storage.googleapis 提供的泰坦尼克号数据集.com/tf-datasets/titanic/train.csv 和 在调用 df.head() 时,我收到了以下输出
survived sex age n_siblings_spouses parch fare class deck embark_town alone
0 male 22.0 1 0 7.2500 Third unknown Southampton n
1 female 38.0 1 0 71.2833 First C Cherbourg n
1 female 26.0 0 0 7.9250 Third unknown Southampton y
1 female 35.0 1 0 53.1000 First C Southampton n
0 male 28.0 0 0 8.4583 Third unknown Queenstown y
但是当我调用 df.columns 时,我得到以下输出
Index(['sex', 'age', 'n_siblings_spouses', 'parch', 'fare', 'class', 'deck',
'embark_town', 'alone'],
dtype='object')
比较 head 时缺少“幸存”列和列法。
我对下一步该做什么感到困惑,因为当我尝试弹出“幸存”列时,它给了我一个 keyError。
输出:
KeyError: 'survived'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 'survived'
So I'm working with the Titanic Dataset provided at https://storage.googleapis.com/tf-datasets/titanic/train.csv and
upon calling df.head()
I recieved the following output
survived sex age n_siblings_spouses parch fare class deck embark_town alone
0 male 22.0 1 0 7.2500 Third unknown Southampton n
1 female 38.0 1 0 71.2833 First C Cherbourg n
1 female 26.0 0 0 7.9250 Third unknown Southampton y
1 female 35.0 1 0 53.1000 First C Southampton n
0 male 28.0 0 0 8.4583 Third unknown Queenstown y
But when I call df.columns
, I get the following as output
Index(['sex', 'age', 'n_siblings_spouses', 'parch', 'fare', 'class', 'deck',
'embark_town', 'alone'],
dtype='object')
The "survived" column is missing when comparing head and columns method.
I'm confused about what to do next because when i try to pop the 'survived' column it gives me a keyError.
Output:
KeyError: 'survived'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 'survived'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我使用了 dataframe = dataframe.reset_index() 方法,错误得到解决。
学分:@BigBen
So I used the dataframe = dataframe.reset_index() method and the error was resolved.
Credits: @BigBen