Python PANDAS: GroupBy First Transform Create Indicator

牧云@^-^@ 提交于 2019-12-11 17:00:36

问题


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.


回答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)


来源:https://stackoverflow.com/questions/48118537/python-pandas-groupby-first-transform-create-indicator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!