1

I have 5 columns, each of them has 0's and 1's in each row. I need to filter all those with '1' at once.

I tried this but results in error:

df_2 = df_1[df_1.columns[0:5] == 1]
ValueError: Item wrong length 2 instead of 111249
1
  • You are comparing with column names. Perhaps you want df_1[df_1.iloc[:, :5] == 1]? Commented Sep 29, 2019 at 4:25

1 Answer 1

1

I believe you need any if want filter at least one 1 per rows of filtered columns:

df_2 = df_1[(df_1.columns[0:5] == 1).any(axis=1)]

Or all if want filter all 1 per rows of filtered columns:

df_2 = df_1[(df_1.columns[0:5] == 1).all(axis=1)]
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.