简体   繁体   中英

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,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 working code:

df_final = df[((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()))].groupby('id').first()

Which results is this:

id,criteria_1,criteria_2,criteria_3,criteria_4,criteria_5,criteria_6
1,1,2,92,120,1,1
2,1,2,90,179,1,1
3,1,2,91,175,1,1

However, I would like to create a new Boolean indicator flag column to indicate which rows meet the criteria (result of above groupby) on the original dataframe using .transform() .

Originally, I thought I could use a combination of .first().transform('any').astype(int) , but I don't think that will work. If there is cleaner way to do this that would be great as well.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM