根据 pandas 中多列中的值从数据框中选择行 [英] Selecting rows from a Dataframe based on values in multiple columns in pandas

查看:64
本文介绍了根据 pandas 中多列中的值从数据框中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与

This question is very related to another, and I'll even use the example from the very helpful accepted solution on that question. Here's the example from the accepted solution (credit to unutbu):

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
print(df)
#      A      B  C   D
# 0  foo    one  0   0
# 1  bar    one  1   2
# 2  foo    two  2   4
# 3  bar  three  3   6
# 4  foo    two  4   8
# 5  bar    two  5  10
# 6  foo    one  6  12
# 7  foo  three  7  14

print(df.loc[df['A'] == 'foo'])

收益

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

但是,如果我想挑选出同时包含"foo"和"one"的所有行怎么办?在这里是第0行和第6行.我的尝试是尝试

But what if I want to pick out all rows that include both 'foo' and 'one'? Here that would be row 0 and 6. My attempt at it is to try

print(df.loc[df['A'] == 'foo' and df['B'] == 'one'])

不幸的是,这不起作用.有人可以建议一种实现这种方式的方法吗?理想情况下,一般而言,其中可能存在涉及andor的一组更复杂的条件,尽管我实际上并不需要此条件.

This does not work, unfortunately. Can anybody suggest a way to implement something like this? Ideally it would be general enough that there could be a more complex set of conditions in there involving and and or, though I don't actually need that for my purposes.

推荐答案

您的代码只需要很小的改动:用&更改and(并添加括号以使比较正确排序):

There is only a very small change needed in your code: change the and with & (and add parentheses for correct ordering of comparisons):

In [104]: df.loc[(df['A'] == 'foo') & (df['B'] == 'one')]
Out[104]:
     A    B  C   D
0  foo  one  0   0
6  foo  one  6  12

必须使用&的原因是,这将对数组进行元素比较,而and希望比较两个计算结果为True或False的表达式.
同样,在需要or比较时,可以在这种情况下使用|.

The reason you have to use & is that this will do the comparison element-wise on arrays, while and expect to compare two expressions that evaluate to True or False.
Similarly, when you want the or comparison, you can use | in this case.

这篇关于根据 pandas 中多列中的值从数据框中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆