1050. 合作过至少三次的演员和导演
2023年8月14日10:35:10
简单
SQL Schema
Pandas Schema
ActorDirector
表:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| actor_id | int |
| director_id | int |
| timestamp | int |
+-------------+---------+
timestamp 是这张表的主键(具有唯一值的列).
编写解决方案找出合作过至少三次的演员和导演的 id 对 (actor_id, director_id)
示例 1:
输入:
ActorDirector 表:
+-------------+-------------+-------------+
| 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 |
+-------------+-------------+-------------+
输出:
+-------------+-------------+
| actor_id | director_id |
+-------------+-------------+
| 1 | 1 |
+-------------+-------------+
解释:
唯一的 id 对是 (1, 1),他们恰好合作了 3 次。
通过次数
55.6K
提交次数
72.6K
通过率
76.5%
答案
import pandas as pd
def actors_and_directors(actor_director: pd.DataFrame) -> pd.DataFrame:
# cnts = actor_director.groupby(['actor_id', 'director_id']).size().reset_index(name='counts')
# return cnts[cnts['counts'] >= 3][['actor_id', 'director_id']]
actor_director = actor_director.groupby(["actor_id","director_id"],as_index=False).agg({"timestamp": lambda x:x.count()>=3})
return actor_director[actor_director["timestamp"]==True][["actor_id","director_id"]]
标签:1050,导演,timestamp,actor,director,三次,id
From: https://www.cnblogs.com/lmq886/p/17628548.html