简体   繁体   English

Python Pandas-根据给定值删除行

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

I think I am close but following error show up: Could you advice what is the reason?我想我很接近但出现以下错误:你能告诉我是什么原因吗?

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

The code is:代码是:

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)

error:错误:

  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'

to remove rows you can use loc :要删除行,您可以使用loc

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

Check out this Pandas Article from may 14 2021.查看 2021 年 5 月 14 日的这篇Pandas 文章

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

Erros message was too long for comment so pasting it in Answer:错误消息太长,无法发表评论,因此将其粘贴到答案中:

  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.

相关问题 Python Pandas-根据给定的窗口并从特定值开始计算特定列的总和 - Python Pandas- Calculate sum of a certain column based on a given window and starting at a certain value Pandas - 根据逻辑语句定位值 - Pandas- locate a value based on logical statements Python Pandas-如何合并行? - Python Pandas- how to merge rows? 如何根据给定条件 pandas/python 删除 *some* 行 - How to remove *some* rows based on a given condition pandas/python 熊猫-根据另一列的行总数创建新列的正确方法(试图在副本上设置的值)? - Pandas- correct way to create a new column based on the sum of rows of another column (value trying to be set on a copy)? python&pandas-根据DataFrame列中的某些值计算出十二行 - python & pandas- Calculation bewteen rows based on certain values in columns from DataFrame Pandas-创建一个基于列值插入新行的表? - Pandas- creating a table that inserts new rows based on column values? Pandas-根据多列值查找平均值 - Pandas- Find average based on multiple column value Python Pandas-根据索引顺序合并两个数据帧 - Python Pandas- Merging two data frames based on an index order Python Pandas:如何根据在该索引处给出的值将分类行转换为二进制行? 下面的例子: - Python Pandas: How do I convert categorical rows into binary rows based on the value given at that index? Example below:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM