Skip to content Skip to sidebar Skip to footer

Pandas: How To Compare Several Cells With A List/tuple

I need to compare some columns in a dataframe as a whole, for example: df = pd.DataFrame({'A':[1,1,3],'B':[4,5,6]}) #Select condition: If df['A'] == 1 and df['B'] == 4, then pick

Solution 1:

Or use [[]] for getting multiple columns:

df[(df[['A','B']].values==[1,4]).all(1)]

Demo:

>>>df = pd.DataFrame({'A':[1,1,3],'B':[4,5,6]})>>>df[(df[['A','B']].values==[1,4]).all(1)]
   A  B
0  1  4
>>>

Solution 2:

You can use a Boolean mask via a NumPy array representation of your data:

df = pd.DataFrame({'A':[1,1,3],'B':[4,5,6]})

res = df[(df.loc[:, 'A':'B'].values == [1, 4]).all(1)]

print(res)

   A  B
0  1  4

In this situation, never combine your columns into a single series of lists. This is inefficient as you will lose all vectorisation benefits, and any processing thereafter will involve Python-level loops.

Post a Comment for "Pandas: How To Compare Several Cells With A List/tuple"