1

I've started learning pandas and have found an issue which I can't seem to resolve. I am loading a data from a csv file and need to delete some rows matching a few strings.

CSV:

id fullname city  tst
999      bbb  CIT  aaa
888      bbb  CIT  aaa
777      xxx  JJJ  aaa

What I've tried:

import pandas as pd

df = pd.read_csv('zzz.csv')

#to_drop = ['xxx', 'aaa']

df = df.drop('xxx',axis=0)

but I am getting the error below:

KeyError: "['xxx'] not found in axis"

What I am missing here? Also, what if I want to pass a list and delete all rows matching strings from the list? Example:

to_drop = ['xxx', 'aaa']

df = df.drop(to_drop,axis=0)
CaioT
  • 1,973
  • 1
  • 11
  • 20
  • As mentioned in the comment above, you can use a boolean expression to choose rows. As for what's wrong with your attempt, is that you did not set an index, which is required by `drop`. Try: `df.set_index("fullname").drop("xxx", axis=0)` – suvayu Jul 26 '21 at 19:24

1 Answers1

1

I suggest:

df = df[df.fullname!='xxx']

and for a list:

names=['xxx','bbb']
df = df[~df.fullname.isin(names)]

the ~ operator means "not"

EMiller
  • 817
  • 1
  • 7
  • 20