Home > Enterprise >  Fill in a column grouped by condition
Fill in a column grouped by condition

Time:11-09

I have table:

user_id days_since_install
001 0
001 1
001 1

It is necessary to check if there is 1 in the column "days_since_install" in grouping by "user_id" and fill in True in the column "retention_1d" otherwise False.

The resulting table should look like this:

user_id retention_1d
001 True

CodePudding user response:

Let us do any

df.groupby('user_id')['days_since_install'].any().reset_index()

CodePudding user response:

One option using groupby.any:

out = (df['days_since_install'].eq(1).groupby(df['user_id']).any()
       .reset_index(name='retention_1d')
      )

Output:

   user_id  retention_1d
0        1          True
  • Related