【问题标题】:I keep getting KeyError我不断收到 KeyError
【发布时间】:2019-08-31 09:02:09
【问题描述】:

谁能帮助解决这个错误?

import pandas as pd
data = pd.read_csv('test', dtype=str)
data.head()
data = data[pd.notnull(data['Sequence'])]

数据看起来像这样。我想删除包含非数字“序列”值的行

Timestamp   Sequence    Others
0   21:04:20.589    1   TS

1   21:04:20.589    Rx  NaN

2   21:04:20.611    2   TS

3   21:04:20.611    Rx  NaN

4   21:04:20.666    3   TS

但我收到此错误消息。我该如何解决这个问题?

KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             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: 'Sequence'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-12-b5f2e388e425> in <module>
      2 data = pd.read_csv('test', dtype=str)
      3 data.head()
----> 4 data = data[pd.notnull(data['Sequence'])]
      5 #data[data[1].apply(lambda x: x.isnumeric())]
      6 #data[pd.to_numeric(data[1], errors='coerce').notnull()]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         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: 'Sequence'

【问题讨论】:

  • 也许列名有一些空格?检查data.columns?

标签: pandas


【解决方案1】:

试试这个:

data = data[data.Sequence.apply(lambda x: x.isnumeric())]

这仅向您显示序列为数字的行,由内置 Python isnumeric() 函数确定。

【讨论】:

    【解决方案2】:

    您可能在某些行中有列表吗?相关问题请参见此处:https://stackoverflow.com/questions/26614465/python-pandas-apply-function-if-a-column-value-is-not-null

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      相关资源
      最近更新 更多