繁体   English   中英

在 Python Dataframe 中删除包含 column1 中另一个特定 column2 上至少一个特定值的所有行

[英]Remove all rows that contains the IDs in column1 that have at least one specific value on another specific column2 in a Python Dataframe

就像标题说的那样,我想删除所有包含 column1 中的 ID 的行,这些行在另一个特定的 column2 上至少具有一个特定值。 在我的情况下,“特定值”为零

   column1 column2 column3
    1   1   a
    1   2   b
    1   3   c
    1   0   d
    2   2   e
    2   3   f
    3   0   g
    3   2   h
    3   0   i
    3   3   j
    4   8   k

而我想要的 output 是

column1 column2 column3
2   2   e
2   3   f
4   8   k

Python 中是否有快速命令可以执行此操作?

非常感谢提前

# get rows whcih contain the tigger (here the zeros in column 2)
df_blacklist = df[df["column2"]==0]
# since you want to remove all IDs in column 1 which have at least 1 zero in column 2
# we get a list of "infected" IDs
lst_blacklisted_ids = list(df_blacklist["column1"].unique())
# now we filter for the IDs which are not infected, be aware that "~" inverts the boolean values in the mask
df = df[~df["column1"].isin(lst_blacklisted_ids)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM