Issue
I have table:
user_id | date | days_since_install |
---|---|---|
001 | 01-01-2021 | 0 |
001 | 02-01-2021 | 1 |
001 | 02-01-2021 | 2 |
It is necessary to check if there is "1" in the column "days_since_install" in grouping by use_id and if so, fill in True in the column "retention_1d" otherwise False.
The resulting table should look like this:
user_id | retention_1d |
---|---|
001 | True |
Solution
You can use groupby.first
to get the first install per group, then map
to map it per user_id:
# get first install value (if you have duplicates you would need to get the min)
d = df[df['event_type'].eq('install')].groupby(df['user_id'])['date'].first()
# map the values per user_id
df['install'] = df['user_id'].map(d)
output:
user_id event_type date install
0 1 install 01-01-2021 01-01-2021
1 1 login 02-01-2021 01-01-2021
2 1 login 04-01-2021 01-01-2021
As a one liner:
df['install'] = df['user_id'].map(df[df['event_type'].eq('install')]
.groupby(df['user_id'])['date'].first())
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.