Using pandas to select rows using two different columns from dataframe?
Q与此类似:使用值列表从熊猫数据帧中选择行
如果两列中的任何一个值在列表中,我希望对其进行数据帧处理。返回两列(结合1和4的结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import numpy as np from pandas import * d = {'one' : [1., 2., 3., 4] ,'two' : [5., 6., 7., 8.],'three' : [9., 16., 17., 18.]} df = DataFrame(d) print df checkList = [1,7] print df[df.one == 1 ]#1 print df[df.one == 7 ]#2 print df[df.two == 1 ]#3 print df[df.two == 7 ]#4 #print df[df.one == 1 or df.two ==7] print df[df.one.isin(checkList)] |
您几乎拥有它,但是您必须使用"按位或"运算符:
1 2 3 4 5 6 7 8 9 10 11 | In [6]: df[(df.one == 1) | (df.two == 7)] Out[6]: one three two 0 1 9 5 2 3 17 7 In [7]: df[(df.one.isin(checkList)) | (df.two.isin(checkList))] Out[7]: one three two 0 1 9 5 2 3 17 7 |