1

I have the following DataFrame and I need to select data that has [1,2,3,4,5] in the [f1,f2,f3,f4,f5] fields respectively.

ID  f1  f2  f3  f4  f5
1   1   2   3   4   5
2   2   3   4   5   6
3   1   2   3   4   5
4   5   4   2   3   4


df = DataFrame(numpy.array([[1, 1, 2, 3, 4, 5],
                            [2, 2, 3, 4, 5, 6],
                            [3, 1, 2, 3, 4, 5],
                            [4, 5, 4, 2, 3, 4]], dtype=int64), 
               columns = ['ID','f1','f2','f3','f4','f5'])

An obvious way is to do the following:

df[(df['f1'] == 1) & (df['f2'] == 2) & (df['f3'] == 3) & (df['f4'] == 4) & (df['f5'] == 5)]

Is there any concise way to do this? I need to do it multiple times and the field names may be different for some other DataFrame.

1 Answer 1

4

A slightly simpler way could be:

>>> df[(df.loc[:, 'f1':'f5'] == np.arange(1, 6)).all(1)]
   ID  f1  f2  f3  f4  f5
0   1   1   2   3   4   5
2   3   1   2   3   4   5

Here df.loc[:, 'f1':'f5'] chooses the columns, and these are tested (row-wise) for equality with the array [1, 2, 3, 4, 5].

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.