【问题标题】:Why does DataFrame row selection syntax df[:2] work but not if the syntax df[1]?为什么 DataFrame 行选择语法 df[:2] 有效,但如果语法 df[1] 则无效?
【发布时间】:2020-12-31 04:56:41
【问题描述】:

我有以下数据:

data = pd.DataFrame(np.arange(16).reshape(4, 4), index = ['Ohio', 'Colorado', 'Utah', 'New York'], columns = ['one', 'two', 'three', 'four'])

如果我运行: data[:2] 输出将是:

           one  two three four
Ohio        0    1    2   3
Colorado    4    5    6   7

如果我运行:data[1],会出现以下错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 1

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-81-c402bf503b75> in <module>
----> 1 data[1]

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 1

为什么我可以运行 data[:2] 而不是 data[1]?这对我来说没有意义。提前谢谢你:-)

【问题讨论】:

    标签: python pandas dataframe slice


    【解决方案1】:

    [ ] - 索引运算符查找输入的列名。

    在您的示例 data[1] 中,没有按名称命名的列 1。所以关键错误。

    但是,当您在索引运算符中传递切片符号 : 时,索引运算符会将行为从 “搜索列” 更改为 “根据范围搜索行”

    【讨论】:

      【解决方案2】:

      第一部分data[:1]是一个切片操作。并且当您设置index = ['Ohio', 'Colorado', 'Utah', 'New York'] 时,没有默认索引(0-9 ..),这就是它给您一个关键错误的原因。

      如果您输入列名。喜欢data['one']你会得到

      俄亥俄州 0

      科罗拉多 4

      犹他州 8

      纽约 12

      名称:一,数据类型:int64

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-06
        • 2020-12-07
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多