Saad
Saad

Reputation: 1360

pandas: how to filter rows by selecting range of columns?

I have the following dataframe:

    name  c1 c2 c3 c4 c5 c6 c7 c8 
    ---   -- -- -- -- -- -- -- --
0   img1  0  1  1  0  0  0  1  0
1   img2  1  0  0  0  0  0  1  1
2   img3  1  0  0  1  0  1  0  0
...

I would like to select those rows that have at least one non-zero value (i.e, 1) in the column range c2 to c6. The resultant dataframe should exclude the second row (img2 ...).

I can solve this problem by mentioning each column separately in the condition:

df = df[((df['c2']==1) | (df['c3']==1) ... | (df['c6']==1))]

Is there any other neater way to achieve the same thing without mentioning each column (possibly based on the range of positions of columns)?

Upvotes: 1

Views: 2251

Answers (2)

oppressionslayer
oppressionslayer

Reputation: 7224

You can do this:

df[df.ix[:,2:7].eq(1).any(axis=1)].ix[:,2:7] 

output ( missing row 1 due to all zero's):

   c2  c3  c4  c5  c6
0   1   1   0   0   0
2   0   0   1   0   1

To show all the columns:

df[df.ix[:,2:7].eq(1).any(axis=1)] 

output:

   name  c1  c2  c3  c4  c5  c6  c7  c8
0  img1   0   1   1   0   0   0   1   0
2  img3   1   0   0   1   0   1   0   0

Upvotes: 1

9mat
9mat

Reputation: 1234

# test data
from io import StringIO
data = StringIO('''name,c1,c2,c3,c4,c5,c6,c7,c8
img1,0,1,1,0,0,0,1,0
img2,1,0,0,0,0,0,1,1
img3,1,0,0,1,0,1,0,0''')

import pandas as pd
df = pd.read_csv(data)


# list of columns to be used

# select using column name
# cols = ['c{}'.format(i) for i in range(2,7)]

# select using column number
cols = df.columns[2:7]

# select if any col is 1
df = df[(df[cols]==1).any(axis=1)]



print(df)

   name  c1  c2  c3  c4  c5  c6  c7  c8
0  img1   0   1   1   0   0   0   1   0
2  img3   1   0   0   1   0   1   0   0

Upvotes: 2

Related Questions