簡體   English   中英

Pandas DataFrame:根據條件刪除行

[英]Pandas DataFrame:Remove rows based on condition

我有 df 列rollnamepresent 我正在嘗試刪除name == marcuspresent == False的數據。

        roll    name    present
    0    123     bruno     False
    1    123     bruno     False
    2    123     bruno     True
    3    78      marcus    False       (Remove this row)
    4    78      marcus    True
    5    78      marcus    True

Output:

        roll    name    present
    0    123     bruno     False
    1    123     bruno     False
    2    123     bruno     True
    4    78      marcus    True
    5    78      marcus    True

是否可以在不拆分 dataframe 的情況下刪除該行

嘗試這個

df = df[~((df.name == 'marcus') & (df.present == False))]

只需做一個選擇:

df = df[(df['name'] != 'marcus') & (df['present'] != False)]

記得檢查 name NOT 等於 marcus 和 present NOT 等於 False 的地方,因為那是你想要保留的!

請試試這個。

df = df[!((df['name'] == 'marcus') && (df['present'] == False))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM