繁体   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