python - 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.
解决方案
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
>>>
推荐阅读
- c++ - 访问在另一个 else 语句中声明的向量
- python - python - 数据框 - groupby - 在有差异的情况下处理非分组列
- excel - 使用基于选定单元格的单元格中的值更改选定单元格的颜色
- php - Wordpress 前端文件上传
- java - Java:我不了解相对路径
- sqlite - sqlite3 - 过滤掉具有相同标识符的第一条和最后一条记录
- flutter - 这个声音问题在颤振应用程序中意味着什么?
- git - 如何在 Mercurial 中获取包含特定 revisoin 的所有分支的列表?
- javascript - 票据转换为较小的票据
- javascript - 获取按钮的空值