python dataframe:删除所有列的值是特定值的行

时间:2018-03-31 04:07:10

标签: python-3.x dataframe

有一个数据框,如下所示:

   id       a            b           c            d              e
    a     23_2_1     34_55_0    34_55_0      -1_-1_-1        34_55_0
    b     3_55_0    34_55_0   34_55_0       34_55_0          34_55_0
    c     -1_-1_-1    34_55_0   34_55_0       34_55_0        -1_-1_-1
    d     34_55_0    -1_-1_-1   34_55_0       34_55_0        34_55_0
    e     34_55_0    34_55_0   34_55_0       34_55_0         34_55_0
    f     34_55_0    34_55_0   34_55_0       34_55_0         34_55_0

我想删除数据框中有列值为'-1_-1_-1'的行,并提取包含'-1_-1_-1'的id。

我的尝试:

lst_col = list(df.columns)[:-1]
df2 = df_bl[~df_bl[lst_col].isin(['-1_-1_-1'])]

1 个答案:

答案 0 :(得分:2)

首先,找到您关心的值:

match_cells = df == '-1_-1_-1'

无论指定的值是什么,它都会为您提供一个TrueFrame,而在其他地方则为False。

现在选择匹配的行:

match_rows = match_cells.any(axis=1)

然后选择没有匹配的行:

df2 = df[~match_rows]

获取带有匹配项的行标签:

match_ids = df.index[match_rows]