Reputation: 1591
I have a pandas dataframe in the following format:
id,criteria_1,criteria_2,criteria_3,criteria_4,criteria_5,criteria_6
1,0,0,95,179,1,1
1,0,0,97,185,NaN,1
1,1,2,92,120,1,1
2,0,0,27,0,1,NaN
2,1,2,90,179,1,1
2,2,5,111,200,1,1
3,1,2,91,175,1,1
3,0,8,90,27,NaN,NaN
3,0,0,22,0,NaN,NaN
I have the following program from Python PANDAS: GroupBy First Transform Create Indicator:
mask = (((df['criteria_1'] >=1.0) | (df['criteria_2'] >=2.0)) &
(df['criteria_3'] >=90.0) &
(df['criteria_4'] <=180.0) &
((df['criteria_5'].notnull()) & (df['criteria_6'].notnull())))
# reset_index() defaults to drop=False. It inserts the old index into the DF
# as a new column named 'index'.
idx = df.reset_index()[mask].groupby('id').first().reset_index(drop=True)['index']
df['flag'] = df.index.isin(idx).astype(int)
However, now I would like to select for any rows where the conditions are met by group-not just the first. It does not appear to be as easy as substituting .any() or .all() for .first(). Any troubleshooting tips would be appreciated!
Upvotes: 1
Views: 406
Reputation: 164783
You can use mask
directly to extract all rows which meet your conditions:
df['flag'] = mask.astype(int)
Remember, that mask
is just a series which returns, for each row, a Boolean value depending on whether all criteria are met.
Upvotes: 1