Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete from a dataframe by condition python

I have a dataframe in pandas with columns QUESTIONS and ANSWERS

| QUESTION | ANSWER |
| -------- | ------ |
| www      | 123    |
| aaa      | 3546   |
| vvv      | 432    |
| ttt      | 455    |
| QUESTION | 534    |
| eee      | 4344   |
| yyy      | 5435   |

I need to delete a row = ‘QUESTIONS’ in column QUESTIONS
I do it this 2 ways but it deletes the whole column

# 1 approach
test_df = test_df.drop("QUESTIONS", axis=1) 
 
# 2 approach
test_df = test_df.set_index("QUESTIONS")
test_df = test_df.drop("QUESTIONS", axis=0) # Delete all rows with label

What is my mistake that it deletes the whole column?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the loc operator

test_df = test_df.loc[~(test_df.QUESTION=='QUESTION')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading