2

I have the following dataframe:

import pandas as pd
import numpy as np
ds = pd.DataFrame({'z':np.random.binomial(n=1,p=0.5,size=10), 
                   'x':np.random.binomial(n=1,p=0.5,size=10), 
                   'u':np.random.binomial(n=1,p=0.5,size=10), 
                   'y':np.random.binomial(n=1,p=0.5,size=10)})
ds
    z   x   u   y
0   0   1   0   0
1   0   1   1   1
2   1   1   1   1
3   0   0   1   1
4   0   0   1   1
5   0   0   0   0
6   1   0   1   1
7   0   1   1   1
8   1   1   0   0
9   0   1   1   1

How do I select rows that have the values (0,1) for variable names specified in a list?

This is what I have thus far:

zs = ['z','x']
tf = ds[ds[zs].values == (0,1)]
tf

Now that prints:

    z   x   u   y
0   0   1   0   0
0   0   1   0   0
1   0   1   1   1
1   0   1   1   1
2   1   1   1   1
3   0   0   1   1
4   0   0   1   1
5   0   0   0   0
7   0   1   1   1
7   0   1   1   1
8   1   1   0   0
9   0   1   1   1
9   0   1   1   1

Which shows duplicates and also has incorrect row (row #2 - 1,1,1,1). Any thoughts or ideas? Of course I am assuming there is a pythonic way of doing this without nested loops and brute-forcing it.

0

4 Answers 4

5

You can use broadcasted numpy comparison:

df[(df[['z','x']].values == [0, 1]).all(1)]

   z  x  u  y
0  0  1  0  0
1  0  1  1  1
7  0  1  1  1
9  0  1  1  1

You can also use np.logical_and.reduce:

cols = ['z', 'x']
vals = [0, 1]

df[np.logical_and.reduce([df[c] == v for c, v in zip(cols, vals)])]

   z  x  u  y
0  0  1  0  0
1  0  1  1  1
7  0  1  1  1
9  0  1  1  1

Lastly, assuming your column names are compatible, dynamically generate query expression strings for use with query:

querystr = ' and '.join([f'{c} == {v!r}' for c,  v in zip(cols, vals)])
df.query(querystr)

   z  x  u  y
0  0  1  0  0
1  0  1  1  1
7  0  1  1  1
9  0  1  1  1

Where {v!r} is the same as {repr(v)}.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @coldspeed. It's very helpful. Do you know why my simplistic query gives the wrong answer (duplicates and an erroneous row)?
@vsm You were close. You need a 1D mask to index df. So, with your solution, you should have done (df[['z', 'x']].values == (0, 1)).all(axis=1) to see which row satisfied this condition for all columns. That's why this was my first option—to show you your fixed code.
1

You can do:

cols = ['u','x']
bools = ds[cols].apply(lambda x: all(x == (0,1)), axis=1)
ds[bools]

   u  x  y  z
0  0  1  1  1
7  0  1  0  1
8  0  1  1  0

Comments

1

Using eq , and very similar to cold's numpy method

df[df[zs].eq(pd.Series([0,1],index=zs),1).all(1)]
   z  x  u  y
0  0  1  0  0
1  0  1  1  1
7  0  1  1  1
9  0  1  1  1

Comments

0

A simpler way is to use boolean indexing:

f = ds['z'] == 0
g = ds['x'] == 1
ds[f & g]

1 Comment

"simple" but does not scale for multiple columns and values. See np.logical_and.reduce on how to generalise it (also, see the second option of my answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.