Bin
Bin

Reputation: 3834

How to create mask with multiple alternative values (Pandas DataFrame)

Suppose we have a Pandas DataFrame f defined as follows. I am trying to create a mask to select all rows with value 'a' or 'b' in column 'xx'(I would like to select out row 0, 1, 3, 4).

f = pd.DataFrame([['a', 'b','c','a', 'b','c'],['1', '2','3', '4', '5','6', ]])
f = f.transpose()
f.columns = ['xx', 'yy']
f

   xx   yy
0   a   1
1   b   2
2   c   3
3   a   4
4   b   5
5   c   6

Is there any elegant way to do this in pandas? I know to select all rows with f.xx =='a', we can do f[f.xx == 'a']. While I have not figure out how to select rows with f.xx is either 'a' or 'b'. Thanks.

Upvotes: 7

Views: 6197

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180540

You could use isin

 print(f[(f["xx"].isin(("a","b")))])

Which will give you:

  xx yy
0  a  1
1  b  2
3  a  4
4  b  5

If you really wanted a mask you could use or |:

mask = (f["xx"] == "a") | (f["xx"] == "b")

print(f[mask])

Which will give you the same output:

 xx yy
0  a  1
1  b  2
3  a  4
4  b  5

Upvotes: 7

Related Questions