我在熊貓中有一個資料框,其中包含 QUESTIONS 和 ANSWERS 列
| QUESTION | ANSWER |
| -------- | ------ |
| www | 123 |
| aaa | 3546 |
| vvv | 432 |
| ttt | 455 |
| QUESTION | 534 |
| eee | 4344 |
| yyy | 5435 |
我需要在 QUESTIONS 列中洗掉一行 = 'QUESTIONS' 我這樣做有兩種方式,但它會洗掉整個列
# 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
它洗掉了整列我的錯誤是什么?
uj5u.com熱心網友回復:
使用loc運算子
test_df = test_df.loc[~(test_df.QUESTION=='QUESTION')]
uj5u.com熱心網友回復:
@Arnau 的方法要好得多。如果你需要使用drop,你可以使用:
out = df.set_index('QUESTION').drop(['QUESTION']).reset_index()
或(這只是在@Arnau 的答案中撰寫方法的一種復雜方式):
out = df.drop(df.loc[df['QUESTION']=='QUESTION'].index)
輸出:
QUESTION ANSWER
0 www 123
1 aaa 3546
2 vvv 432
3 ttt 455
4 eee 4344
5 yyy 5435
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453881.html
