Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine pandas DataFrame query() method with isin()

So I want to use isin() method with df.query(), to select rows with id in a list: id_list. Similar question was asked before, but they used typical df[df['id'].isin(id_list)] method. I'm wondering if there is a way to use df.query() instead.

df = pd.DataFrame({'a': list('aabbccddeeff'), 'b': list('aaaabbbbcccc'),                    'c': np.random.randint(5, size=12),                    'd': np.random.randint(9, size=12)})  id_list = ["a", "b", "c"] 

And this yields an error

df.query('a == id_list') 
like image 557
user4015990 Avatar asked Nov 30 '15 03:11

user4015990


People also ask

How do you ISIN a DataFrame in Python?

DataFrame - isin() functionThe isin() function is used to check each element in the DataFrame is contained in values or not. The result will only be true at a location if all the labels match. If values is a Series, that's the index. If values is a dict, the keys must be the column names, which must match.

How do you combine two series in a data frame?

append() to Combine Two Series. You can use pandas. DataFrame(Series. append(Series,ignore_index=True)) to create a DataFrame by appending series to another series.


2 Answers

You can also include the list within the query string:

>>> df.query('a in ["a", "b", "c"]') 

This is the same as:

>>> df.query('a in @id_list') 
like image 137
Seiji Armstrong Avatar answered Sep 22 '22 14:09

Seiji Armstrong


From the docs for query

You can refer to variables in the environment by prefixing them with an '@' character like @a + b.

In your case:

In [38]: df.query('a == @id_list') Out[38]:    a  b  c  d 0  a  a  3  4 1  a  a  4  5 2  b  a  2  3 3  b  a  1  5 4  c  b  2  4 5  c  b  1  2 
like image 28
maxymoo Avatar answered Sep 19 '22 14:09

maxymoo