[문제]
https://leetcode.com/problems/user-activity-for-the-past-30-days-i/description/
[풀이]
1. 특정 기간 사이의 행들만 필터링 (between 메소드 사용)
2. 날짜별로 그룹화해서 고유 유저 수를 센다 (nunique 메소드 사용)
3. 컬럼명 변경 (rename 메소드 사용)
Pandas
import pandas as pd
def user_activity(activity: pd.DataFrame) -> pd.DataFrame:
return activity[activity['activity_date'].between('2019-06-28', '2019-07-27')].groupby('activity_date')['user_id'].nunique().reset_index().rename(columns={'activity_date':'day', 'user_id':'active_users'})
SQL
SELECT
activity_date AS day
, COUNT(DISTINCT user_id) AS active_users
FROM Activity
WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27'
GROUP BY activity_date
'코딩 테스트 > Pandas' 카테고리의 다른 글
102. Classes More Than 5 Students (0) | 2025.01.07 |
---|---|
101. Product Sales Analysis III (0) | 2025.01.06 |
Game Play Analysis IV (0) | 2025.01.01 |
Immediate Food Delivery II (0) | 2024.12.31 |
Monthly Transactions I (0) | 2024.12.30 |