首页 > 解决方案 > 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 up this row. 

For this simple example, I can use below method:

df.loc[(df['A']==1)&(df['B']==4),'A':'B']

However, in reality my dataframe has tens of columns which should be compared as whole. Above solution will be very very messy if I choose to list all of them. So I think if regard them as a whole to compare with a list may solve the problem:

#something just like this:
df.loc[df.loc[:,'A':'B']==[1,4],'A':'B')]

Not worked. So I came up with the idea that first combine all desired columns into a new column as a list value, then compare this new column with the list. The latter has been solved in Pandas: compare list objects in Series

Although generally I've solved my case, I still want to know if there is an easier way to solve this problem? Thanks.

标签: pythonpandas

解决方案


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
>>> 

推荐阅读