2

I have some colums in my DataFrame with values 0 and 1

    name    a   b   c   d   e
0   one     1   0   1   0   0
1   two     0   0   1   0   0
2   three   0   0   1   0   1

How can I select columns where at least one value is 1? But another columns (that are strings or take not only 0 and 1 values) must be selected too.

I tried this expression

df.iloc[:, [(clm == 'name') | (1 in df[clm].unique()) for clm in df.columns]]

Out:
    name  a  c  e
0    one  1  1  0
1    two  0  1  0
2  three  0  1  1

But is seems not good because I explicitly choose column 'name'

1 Answer 1

3

If is possible remove all columns with only 0 values compare values by DataFrame.ne for not equal and return at least one True per columns in DataFrame.loc:

df = df.loc[:, df.ne(0).any()]
print (df)
    name  a  c  e
0    one  1  1  0
1    two  0  1  0
2  three  0  1  1

Details:

print (df.ne(0))
   name      a      b     c      d      e
0  True   True  False  True  False  False
1  True  False  False  True  False  False
2  True  False  False  True  False   True

print (df.ne(0).any())
name     True
a        True
b       False
c        True
d       False
e        True
dtype: bool
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this good solution. What if I want to select columns with value 1? Or I have columns with another values but I should apply condition only for not string columns?
If I want to select columns where any value is 1 df.eq(1).any(). How select column 'name' too?
@sergzemsk Not tested, I am on phone only. But one trick should be select all numeric columns, test 1 and then add all another columns, with strings. Like df = df.loc[:, df.select_dtypes(np.number).eq(1).any().reindex(df.columns, fill_value=True)]

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.