top of page

Customer Placing the Largest Number of Orders

LeetCode Problem 586

Customer Placing the Largest Number of Orders
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.

import pandas as pd

data = [[1, 1], [2, 2], [3, 3], [4, 3]]
orders = pd.DataFrame(data, columns=['order_number', 'customer_number']).astype({'order_number':'Int64', 'customer_number':'Int64'})

def largest_orders(orders: pd.DataFrame) -> pd.DataFrame:
    grouped = orders.groupby('customer_number').count().reset_index()
    sort = orders.sort_values(by='order_number', ascending= False).head(1)
    sort = sort[["customer_number"]]
    return sort

result = largest_orders(orders)
print(result)



#OR


def largest_orders(orders: pd.DataFrame) -> pd.DataFrame:
    return orders['customer_number'].mode().to_frame()

result = largest_orders(orders)
print(result)

bottom of page