有一个数据框,如下所示:
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'])]
首先,找到你关心的价值观:
match_cells = df == '-1_-1_-1'
无论指示值在哪里,这都会为您提供一个带有 True 的 DataFrame ,而在其他地方则为 False 。
现在选择匹配的行:
match_rows = match_cells.any(axis=1)
然后取没有匹配项的行:
df2 = df[~match_rows]
并获取匹配的行标签:
match_ids = df.index[match_rows]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句