Table: ActorDirector
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| actor_id | int |
| director_id | int |
| timestamp | int |
+-------------+---------+
timestamp는 이 테이블의 기본 키(고유 값을 가진 열)입니다.
문제
배우가 감독과 최소 3번 이상 협업한 모든 (actor_id, director_id) 쌍을 찾는 해결책을 작성하십시오.
결과 테이블은 아무 순서로 반환해도 됩니다.
결과 형식은 아래 예시와 같습니다.
Example 1:
Input:
ActorDirector table:
+-------------+-------------+-------------+
| actor_id | director_id | timestamp |
+-------------+-------------+-------------+
| 1 | 1 | 0 |
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 1 | 2 | 4 |
| 2 | 1 | 5 |
| 2 | 1 | 6 |
+-------------+-------------+-------------+
Output:
+-------------+-------------+
| actor_id | director_id |
+-------------+-------------+
| 1 | 1 |
+-------------+-------------+
설명 : 유일한 쌍은 (1, 1)로, 이들은 정확히 3번 협업했습니다.
✏️ 풀이
import pandas as pd
def actors_and_directors(actor_director: pd.DataFrame) -> pd.DataFrame:
df = actor_director.groupby(['actor_id', 'director_id'])['director_id'].count().reset_index(name='cnt')
return df[df['cnt'] >= 3][['actor_id', 'director_id']]
1. actor_id, director_id 별 협업 수 카운트
2. 카운트 수가 3번 이상인 데이터만 조회
'[코딩테스트] > [Pandas]' 카테고리의 다른 글
[LeetCode - Pandas] (Easy) 620. Not Boring Movies (0) | 2025.04.25 |
---|---|
[LeetCode - Pandas] (Easy) 627. Swap Salary (0) | 2025.04.24 |
[LeetCode - Pandas] (Easy) 1068. Product Sales Analysis I (0) | 2025.04.23 |
[LeetCode - Pandas] (Easy) 1075. Project Employees I (0) | 2025.04.23 |
[LeetCode - Pandas] (Easy) 1084.Sales Analysis III (0) | 2025.04.22 |