본문 바로가기

Algorithm/Python

[stratascratch] Highest Number Of Orders(Python)

문제

Find the customer who has placed the highest number of orders. Output the id of the customer along with the corresponding number of orders.

  • 가장 많은 수의 주문을 한 고객을 찾아라
  • 고객의 ID를 해당 주문 수와 함께 출력

코드

# Import your libraries
import pandas as pd

# Start writing code
customers.head()
# customers와 orders 조인
df = pd.merge(customers, orders, how="inner", left_on="id", right_on="cust_id")
# id_x 별로 count 집계
df = df.groupby('id_x')['cust_id'].count().reset_index()
df = df[df['cust_id'] == (df['cust_id'].max())]

출처

반응형