Python PANDAS: GroupBy First Transform Create Indicator
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,2
Solution 1:
Here's one way:
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)
Post a Comment for "Python PANDAS: GroupBy First Transform Create Indicator"