본문 바로가기

Algorithm/Python

[stratascratch] Customer Details(Python)

문제

Find the details of each customer regardless of whether the customer made an order. Output the customer's first name, last name, and the city along with the order details. Your output should be listing the customer's orders not necessarily listing the customers. This means that you may have duplicate rows in your results due to a customer ordering several of the same items. Sort records based on the customer's first name and the order details in ascending order.

고객의 주문 여부와 상관없이 각 고객의 세부 정보 조회하기
고객의 이름, 성, 도시를 주문 세부 정보와 함께 출력
고객이 동일한 항목을 여러 개 주문했기 때문에 결과에 중복된 행이 있을 수 있다
고객의 이름과 주문 세부 정보를 기준으로 레코드를 오름차순으로 정렬

코드

# Import your libraries
import pandas as pd

# Start writing code
result = pd.merge(customers, orders, how="left", left_on="id", right_on="cust_id").sort_values(['first_name','order_details'])
result[["first_name", "last_name", "city", "order_details"]]

출처

반응형