0

So I want to delete some rows based on a condition.

I tried to drop as it is explained in the website.

My dataframe looks like this:

jobs
   job_name  number
0  job1      163
1  job2      200
2  job3      3

And I have a list:

my_jobs = [job2, job3]

My condition:

for job in jobs['job_name']:
    if job not in my_jobs:
        jobs_df.drop(job, axis=0)     

And I want my dataframe looks like this:

jobs
   job_name  number
1  job2      200
2  job3      3

However, I'm getting:

keyerror:"['jo1'] not found in axis"

What am I doing wrong?

2
  • jobs_df = jobs_df[jobs_df['job_name'].isin(my_jobs)]. Commented Jul 8, 2019 at 18:49
  • @QuangHoang Thank you!! Commented Jul 8, 2019 at 19:13

1 Answer 1

1

I believe the cause of the error is that it is looking for the job_name in the dataframe index which only has [0, 1, 2] you could set the column job_name as index:

jobs_df.set_index("job_name", inplace=True)

and then do:

my_jobs = ["job2", "job3"]
for job in jobs_df.index:
    if job not in my_jobs:
        jobs_df.drop(job, axis=0, inplace=True)
jobs_df.reset_index(inplace=True)

but an easier and faster way to do this would be to just do:

jobs_df = jobs_df[jobs_df["job_name"].isin(my_jobs)]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, is ~ necessary? The code worked, however I think it did not give me the correct result.. I assume ~ means No?
@sarahkim You are right, the ~ means not. You do not need it. it is just a mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.