簡體   English   中英

Python Pandas-根據給定值刪除行

[英]Python Pandas- remove rows based on given value

我想我很接近但出現以下錯誤:你能告訴我是什么原因嗎?

raise KeyError(key) from err KeyError: 'DATE OF OPERATION'

代碼是:

import pandas as pd
from pathlib import Path
source_files = sorted(Path(r'/Users/user/Downloads/').glob('*.csv'))

for file in source_files:
 df = pd.read_csv(file)
 #df.columns = df.columns.str.replace(' ', '_')
 df = df[~df['DATE OF OPERATION'].astype(str).str.startswith('202110')]
 #df.columns = df.columns.str.replace('_', ' ')
 name, ext = file.name.split('.')
 df.to_csv(f'{name}.{ext}', index=0)

錯誤:

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'DATE OF OPERATION'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/user/PycharmProjects/ShareOpe/ShareOpe.py", line 11, in <module>
    df = df.loc[~df['DATE OF OPERATION'].astype(str).str.startswith('202110')]
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py", line 3458, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc
    raise KeyError(key) from err
KeyError: 'DATE OF OPERATION'

要刪除行,您可以使用loc

df = df.loc[~df['DATE OF OPERATION'].astype(str).startswith('202110')]

查看 2021 年 5 月 14 日的這篇Pandas 文章

#drop rows that contain specific 'value' in 'column_name'
df = df[df.your_column_name != value_to_remove]

錯誤消息太長,無法發表評論,因此將其粘貼到答案中:

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'DATE OF OPERATION'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/user/PycharmProjects/ShareOpe/ShareOpe.py", line 11, in <module>
    df = df.loc[~df['DATE OF OPERATION'].astype(str).str.startswith('202110')]
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py", line 3458, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc
    raise KeyError(key) from err
KeyError: 'DATE OF OPERATION'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM