File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
LeetCode SQL 50 Solution/550. Game Play Analysis IV Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1+ import pandas as pd
2+
3+ def game_play_analysis (activity : pd .DataFrame ) -> pd .DataFrame :
4+ # Get first login date for each player
5+ first_login = activity .groupby ("player_id" )["event_date" ].min ().reset_index ()
6+ first_login .columns = ["player_id" , "first_login" ]
7+
8+ # Merge first login date with original table
9+ merged = activity .merge (first_login , on = "player_id" )
10+
11+ # Filter players who logged in the next day
12+ next_day_logins = merged [
13+ (merged ["event_date" ] - merged ["first_login" ]).dt .days == 1
14+ ]["player_id" ].nunique ()
15+
16+ # Total unique players
17+ total_players = activity ["player_id" ].nunique ()
18+
19+ # Calculate fraction
20+ fraction = round (next_day_logins / total_players , 2 )
21+
22+ return pd .DataFrame ({"fraction" : [fraction ]})
You can’t perform that action at this time.
0 commit comments