728x90
문제
Table: Orders
+-----------------+----------+
| Column Name | Type |
+-----------------+----------+
| order_number | int |
| customer_number | int |
+-----------------+----------+
order_number is the primary key (column with unique values) for this table.
This table contains information about the order ID and the customer ID.
Write a solution to find the customer_number for the customer who has placed the largest number of orders.
The test cases are generated so that exactly one customer will have placed more orders than any other customer.
예시
Input:
Orders table:
+--------------+-----------------+
| order_number | customer_number |
+--------------+-----------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 3 |
+--------------+-----------------+
Output:
+-----------------+
| customer_number |
+-----------------+
| 3 |
+-----------------+
Explanation:
The customer with number 3 has two orders, which is greater than either customer 1 or 2 because each of them only has one order.
So the result is customer_number 3.
문제 풀이
* 오답코드
import pandas as pd
def largest_orders(orders: pd.DataFrame) -> pd.DataFrame:
order_counts = orders.groupby(by=['customer_number'])['order_number'].count().reset_index()
result = order_counts.loc[[order_counts['order_number'].idxmax()],['customer_number']]
return result
빈 DataFrame이 주어졌을 때 에러
idxmax()는 최댓값을 가지는 첫번째 행의 인덱스를 반환하기 때문
* 정답코드
import pandas as pd
def largest_orders(orders: pd.DataFrame) -> pd.DataFrame:
order_counts = orders.groupby(by=['customer_number'])['order_number'].count().reset_index()
result = order_counts.loc[order_counts['order_number'].max() == order_counts['order_number'],['customer_number']]
return result
파이썬을 독학하시는 분들에게 도움이 되길 바라며,
혹 더 좋은 방법이 있거나 오류가 있다면 편하게 말씀 부탁드립니다.
728x90