【报错】Python中选取单列或者多列

选取单列

1、以下报错出现是因为字段错误,这里的司机ID是不在报表内

周故意明细 = 周报['ID']
Traceback (most recent call last):

  File "<ipython-input-14-c23e3b5f93d6>", line 1, in <module>
    周故意明细 = 周报['ID']

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2059, in __getitem__
    return self._getitem_column(key)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column
    return self._get_item_cache(key)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1386, in _get_item_cache
    values = self._data.get(item)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3543, in get
    loc = self.items.get_loc(item)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 2136, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)

  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)

  File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)

  File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)

KeyError: 'ID'

正确的

周故意明细 = 周报['姓名']

选取多列

周趴机场司机明细 = 周报['司机姓名','驾管姓名','司机所在劳务公司','车辆级别','专车完单数','机场单完单数','机场占比']
Traceback (most recent call last):

  File "<ipython-input-16-beef4fdecd1a>", line 1, in <module>
    周趴机场司机明细 = 周报['司机姓名','驾管姓名','司机所在劳务公司','车辆级别','专车完单数','机场单完单数','机场占比']

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2059, in __getitem__
    return self._getitem_column(key)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column
    return self._get_item_cache(key)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1386, in _get_item_cache
    values = self._data.get(item)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3543, in get
    loc = self.items.get_loc(item)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 2136, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)

  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)

  File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)

  File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)

KeyError: ('司机姓名', '驾管姓名', '司机所在劳务公司', '车辆级别', '专车完单数', '机场单完单数', '机场占比')

这里的错误是因为,是因为选取多列是要使用双中括号。
正确示例:

周趴机场司机明细 = 周报[['司机姓名','驾管姓名','司机所在劳务公司','车辆级别','专车完单数','机场单完单数','机场占比']]

选取列并条件筛选

周趴机场司机明细 = 周报[['司机姓名','驾管姓名','司机所在劳务公司','车辆级别','专车完单数','机场单完单数','机场占比']][周报['机场占比']>=0.5]
发布了105 篇原创文章 · 获赞 18 · 访问量 16万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览