Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
df_1[df_1.iloc[:, :5] == 1]
I believe you need any if want filter at least one 1 per rows of filtered columns:
any
1
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:
all
df_2 = df_1[(df_1.columns[0:5] == 1).all(axis=1)]
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
df_1[df_1.iloc[:, :5] == 1]?