比较 df.head 和 df.columns 中的列时没有匹配项

发布于 2025-01-15 03:52:14 字数 1522 浏览 2 评论 0原文

因此,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2025-01-22 03:52:14

所以我使用了 dataframe = dataframe.reset_index() 方法,错误得到解决。

学分:@BigBen

So I used the dataframe = dataframe.reset_index() method and the error was resolved.

Credits: @BigBen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文