0

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?

2 Answers 2

2

Use the loc operator

test_df = test_df.loc[~(test_df.QUESTION=='QUESTION')] 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but if I want to use method drop(), what was my mistake?
You need to use .reset_index() after the drop opperation in the second approach
1

@Arnau's method is much better. If you need to use drop, you could use:

out = df.set_index('QUESTION').drop(['QUESTION']).reset_index() 

or (which is just a convoluted way to write the method in @Arnau's answer):

out = df.drop(df.loc[df['QUESTION']=='QUESTION'].index) 

Output:

 QUESTION ANSWER 0 www 123 1 aaa 3546 2 vvv 432 3 ttt 455 4 eee 4344 5 yyy 5435 

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.