Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get subsection of df based on multiple conditions

I'm trying to extract rows from a df based on multiple conditions, ALL of the conditions must be met before any rows are selected else nothing.

My df

columns = ['is_net', 'is_pct', 'is_mean', 'is_wgted', 'is_sum']
index = ['a','b','c','d']
data = [['True','True','False','False', 'False'],
       ['True','True','True','False', 'False'],
       ['True','True','False','False', 'True'],
       ['True','True','False','True', 'False']]

df = pd.DataFrame(columns=columns, index=index, data=data)
df

    is_net  is_pct  is_mean is_wgted    is_sum
a   True    True    False   False   False
b   True    True    True    False   False
c   True    True    False   False   True
d   True    True    False   True    False

My conditions which must be in this format:

conditions = {'is_net': 'True', 
              'is_pct': 'True', 
              'is_mean': 'False', 
              'is_wgted': 'True', 
              'is_sum': 'False'}

Expected output:

    is_net  is_pct  is_mean is_wgted    is_sum
d   True    True    False   True    False
like image 613
Boosted_d16 Avatar asked Jan 11 '16 16:01

Boosted_d16


1 Answers

I think the trick is to make conditions into a Series:

>>> pd.Series(conditions)
is_mean     False
is_net       True
is_pct       True
is_sum      False
is_wgted     True
dtype: object
>>> (df == pd.Series(conditions))
  is_mean is_net is_pct is_sum is_wgted
a    True   True   True   True    False
b   False   True   True   True    False
c    True   True   True  False    False
d    True   True   True   True     True
>>> (df == pd.Series(conditions)).all(axis=1)
a    False
b    False
c    False
d     True
dtype: bool
>>> df[(df == pd.Series(conditions)).all(axis=1)]
  is_net is_pct is_mean is_wgted is_sum
d   True   True   False     True  False
like image 84
DSM Avatar answered Oct 14 '22 05:10

DSM