How to use a dict to subset a DataFrame?
categorical-data, dataframe, pandas, python
Solution
I would use .query() method for this task:
qry = ' and '.join(["{} == '{}'".format(k,v) for k,v in tmp.items()])
data.query(qry)
output:
age risk sex smoking
7 24 no female yes
22 43 no female yes
23 42 no female yes
25 24 no female yes
32 29 no female yes
40 34 no female yes
43 35 no female yes
Query string:
print(qry)
"sex == 'female' and risk == 'no' and smoking == 'yes'"
Problem
Say, I have given a DataFrame with most of the columns being categorical data. ``` > data.head() age risk sex smoking 0 28 no male no 1 58 no female no 2 27 no male yes 3 26 no male no 4 29 yes female yes ``` And I would like to subset this data by a dict of key-value pairs for those categorical variables. ``` tmp = {'risk':'no', 'smoking':'yes', 'sex':'female'} ``` Hence, I would like to have the following subset. ``` data[ (data.risk == 'no') & (data.smoking == 'yes') & (data.sex == 'female')] ``` What I want to do is: ``` data[tmp] ``` What is the most python / pandas way of doing this? Minimal example: ``` import numpy as np import pandas as pd from pandas import Series, DataFrame x = Series(random.randint(0,2,50), dtype='category') x.cat.categories = ['no', 'yes'] y = Series(random.randint(0,2,50), dtype='category') y.cat.categories = ['no', 'yes'] z = Series(random.randint(0,2,50), dtype='category') z.cat.categories = ['male', 'female'] a = Series(random.randint(20,60,50), dtype='category') data = DataFrame({'risk':x, 'smoking':y, 'sex':z, 'age':a}) tmp = {'risk':'no', 'smoking':'yes', 'sex':'female'} ```