728x90
문제
Table: DailySales
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| date_id | date |
| make_name | varchar |
| lead_id | int |
| partner_id | int |
+-------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
This table contains the date and the name of the product sold and the IDs of the lead and partner it was sold to.
The name consists of only lowercase English letters.
For each date_id and make_name, find the number of distinct lead_id's and distinct partner_id's.
Return the result table in any order.
https://leetcode.com/problems/daily-leads-and-partners/description/?lang=pythondata
예시
Input:
DailySales table:
+-----------+-----------+---------+------------+
| date_id | make_name | lead_id | partner_id |
+-----------+-----------+---------+------------+
| 2020-12-8 | toyota | 0 | 1 |
| 2020-12-8 | toyota | 1 | 0 |
| 2020-12-8 | toyota | 1 | 2 |
| 2020-12-7 | toyota | 0 | 2 |
| 2020-12-7 | toyota | 0 | 1 |
| 2020-12-8 | honda | 1 | 2 |
| 2020-12-8 | honda | 2 | 1 |
| 2020-12-7 | honda | 0 | 1 |
| 2020-12-7 | honda | 1 | 2 |
| 2020-12-7 | honda | 2 | 1 |
+-----------+-----------+---------+------------+
Output:
+-----------+-----------+--------------+-----------------+
| date_id | make_name | unique_leads | unique_partners |
+-----------+-----------+--------------+-----------------+
| 2020-12-8 | toyota | 2 | 3 |
| 2020-12-7 | toyota | 1 | 2 |
| 2020-12-8 | honda | 2 | 2 |
| 2020-12-7 | honda | 3 | 2 |
+-----------+-----------+--------------+-----------------+
Explanation:
For 2020-12-8, toyota gets leads = [0, 1] and partners = [0, 1, 2] while honda gets leads = [1, 2] and partners = [1, 2].
For 2020-12-7, toyota gets leads = [0] and partners = [1, 2] while honda gets leads = [0, 1, 2] and partners = [1, 2].
문제 풀이
import pandas as pd
def daily_leads_and_partners(daily_sales: pd.DataFrame) -> pd.DataFrame:
# 'date_id'와 'make_name' 열 기준으로 그룹
# 각 그룹에서 'lead_id'와 'partner_id'의 고유값 개수 계산
grouped = daily_sales.groupby(['date_id', 'make_name'], as_index=False).agg({'lead_id':'nunique', 'partner_id':'nunique'})
# 'lead_id'와 'partner_id' 열의 이름을 각각 'unique_leads'와 'unique_partners'로 변경
result = grouped.rename(columns={'lead_id':'unique_leads', 'partner_id':'unique_partners'})
return result
import pandas as pd
def daily_leads_and_partners(daily_sales: pd.DataFrame) -> pd.DataFrame:
# 'date_id'와 'make_name' 열 기준으로 그룹
# 각 그룹에서 'lead_id'와 'partner_id'의 고유값 개수 계산
# 'lead_id'와 'partner_id' 열의 이름을 각각 'unique_leads'와 'unique_partners'로 변경
return daily_sales.groupby(['date_id', 'make_name'], as_index=False).agg(unique_leads=('lead_id', 'nunique'), unique_partners=('partner_id', 'nunique'))
파이썬을 독학하시는 분들에게 도움이 되길 바라며,
혹 더 좋은 방법이 있거나 오류가 있다면 편하게 말씀 부탁드립니다.
728x90