코딩 테스트/Pandas

103. Find Followers Count

susinlee 2025. 1. 8. 09:23

[문제]

https://leetcode.com/problems/find-followers-count/description/

 

 

[풀이]

1. user_id 별로 그룹화 후 follower_id 수를 센다

2. user_id 로 오름차순 정렬해준다

 

 

Pandas

import pandas as pd

def count_followers(followers: pd.DataFrame) -> pd.DataFrame:
    
    return followers.groupby('user_id').size().reset_index(name='followers_count').sort_values('user_id')

 

SQL

SELECT 
    user_id
    , COUNT(follower_id) as followers_count
FROM Followers
GROUP BY user_id
ORDER BY user_id

'코딩 테스트 > Pandas' 카테고리의 다른 글

102. Classes More Than 5 Students  (0) 2025.01.07
101. Product Sales Analysis III  (0) 2025.01.06
100. User Activity for the Past 30 Days I  (0) 2025.01.05
Game Play Analysis IV  (0) 2025.01.01
Immediate Food Delivery II  (0) 2024.12.31