Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Check if row exists with certain values

I have a two dimensional (or more) pandas DataFrame like this:

>>> import pandas as pd >>> df = pd.DataFrame([[0,1],[2,3],[4,5]], columns=['A', 'B']) >>> df    A  B 0  0  1 1  2  3 2  4  5 

Now suppose I have a numpy array like np.array([2,3]) and want to check if there is any row in df that matches with the contents of my array. Here the answer should obviously true but eg. np.array([1,2]) should return false as there is no row with both 1 in column A and 2 in column B.

Sure this is easy but don't see it right now.

like image 613
Robin Avatar asked Jul 15 '14 14:07

Robin


People also ask

How do you check if a row contains a value pandas?

You can see how we can determine a pandas column contains a particular value of DataFrame using Series. Str. contains() . This contains() function is used to test the pattern or regex is contained within a string of a Series or Index.

How do I search for a specific row in Python?

In the Pandas DataFrame we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter.


1 Answers

Turns out it is really easy, the following does the job here:

>>> ((df['A'] == 2) & (df['B'] == 3)).any() True >>> ((df['A'] == 1) & (df['B'] == 2)).any() False 

Maybe somebody comes up with a better solution which allows directly passing in the array and the list of columns to match.

Note that the parenthesis around df['A'] == 2 are not optional since the & operator binds just as strong as the == operator.

like image 107
Robin Avatar answered Sep 22 '22 02:09

Robin