有人可以帮助解决此错误吗?
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'
答案 0 :(得分:0)
尝试一下:
data = data[data.Sequence.apply(lambda x: x.isnumeric())]
这仅向您显示Sequence(序列)为数字的行,这由内置的Python isnumeric()函数确定。
答案 1 :(得分:0)