Home > Software design >  Python Pandas- remove rows based on given value
Python Pandas- remove rows based on given value

Time:01-08

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'

The code is:

import pandas as pd
from pathlib import Path
source_files = sorted(Path(r'/Users/maciejgrzeszczuk/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)

CodePudding user response:

to remove rows you can use loc:

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

CodePudding user response:

Check out this Pandas Article from may 14 2021.

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