[英]select rows in pandas DataFrame using comparisons against two columns
[英]Using pandas to select rows using two different columns from dataframe?
Q類似於: 使用值列表從pandas數據幀中選擇行
如果兩列中的任何一個值都在列表中,我想要數據幀。 返回兩列(合並#1和#4的結果。
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)]
你幾乎擁有它,但你必須使用“按位或”運算符:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.