0

There is a dataframe, like the following :

   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

I want to delete the rows that if there is value of columns is '-1_-1_-1' in the dataframe, and also extract the id that include the '-1_-1_-1'.

My trying:

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

1 Answer 1

2

First, find the values you care about:

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

That gives you a DataFrame with True wherever the indicated value is, and False elsewhere.

Now select the rows that have a match:

match_rows = match_cells.any(axis=1)

Then take the rows without matches:

df2 = df[~match_rows]

And get the row labels with matches:

match_ids = df.index[match_rows]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.