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?
CodePudding user response:
Use the loc
operator
test_df = test_df.loc[~(test_df.QUESTION=='QUESTION')]
CodePudding user response:
@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