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)
1
  • 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) Commented Jul 26, 2021 at 19:24

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.