diff --git a/solution/1500-1599/1565.Unique Orders and Customers Per Month/README.md b/solution/1500-1599/1565.Unique Orders and Customers Per Month/README.md index 1cbe5036fd543..34192f127d5fb 100644 --- a/solution/1500-1599/1565.Unique Orders and Customers Per Month/README.md +++ b/solution/1500-1599/1565.Unique Orders and Customers Per Month/README.md @@ -82,7 +82,9 @@ Orders -### 方法一 +### 方法一:条件筛选 + 分组统计 + +我们可以先筛选出金额大于 $20$ 的订单,然后按月份进行分组统计订单数和顾客数。 @@ -96,7 +98,28 @@ SELECT COUNT(DISTINCT customer_id) AS customer_count FROM Orders WHERE invoice > 20 -GROUP BY month; +GROUP BY 1; +``` + +#### Pandas + +```python +import pandas as pd + + +def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame: + filtered_orders = orders[orders["invoice"] > 20] + filtered_orders["month"] = ( + filtered_orders["order_date"].dt.to_period("M").astype(str) + ) + result = ( + filtered_orders.groupby("month") + .agg( + order_count=("order_id", "count"), customer_count=("customer_id", "nunique") + ) + .reset_index() + ) + return result ``` diff --git a/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md b/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md index fddd57092b387..17f2788eee346 100644 --- a/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md +++ b/solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md @@ -43,7 +43,7 @@ This table contains information about the orders made by customer_id.

Example 1:

-Input: 
+Input:
 Orders table:
 +----------+------------+-------------+------------+
 | order_id | order_date | customer_id | invoice    |
@@ -59,7 +59,7 @@ Orders table:
 | 9        | 2021-01-07 | 3           | 31         |
 | 10       | 2021-01-15 | 2           | 20         |
 +----------+------------+-------------+------------+
-Output: 
+Output:
 +---------+-------------+----------------+
 | month   | order_count | customer_count |
 +---------+-------------+----------------+
@@ -68,7 +68,7 @@ Orders table:
 | 2020-12 | 2           | 1              |
 | 2021-01 | 1           | 1              |
 +---------+-------------+----------------+
-Explanation: 
+Explanation:
 In September 2020 we have two orders from 2 different customers with invoices > $20.
 In October 2020 we have two orders from 1 customer, and only one of the two orders has invoice > $20.
 In November 2020 we have two orders from 2 different customers but invoices < $20, so we don't include that month.
@@ -82,7 +82,9 @@ In January 2021 we have two orders from 2 different customers, but only one of t
 
 
 
-### Solution 1
+### Solution 1: Conditional Filtering + Grouping Statistics
+
+We can first filter out orders with an amount greater than $20$, and then group by month to count the number of orders and customers.
 
 
 
@@ -96,7 +98,28 @@ SELECT
     COUNT(DISTINCT customer_id) AS customer_count
 FROM Orders
 WHERE invoice > 20
-GROUP BY month;
+GROUP BY 1;
+```
+
+#### Pandas
+
+```python
+import pandas as pd
+
+
+def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
+    filtered_orders = orders[orders["invoice"] > 20]
+    filtered_orders["month"] = (
+        filtered_orders["order_date"].dt.to_period("M").astype(str)
+    )
+    result = (
+        filtered_orders.groupby("month")
+        .agg(
+            order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
+        )
+        .reset_index()
+    )
+    return result
 ```
 
 
diff --git a/solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.py b/solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.py
new file mode 100644
index 0000000000000..a7db3f22ec561
--- /dev/null
+++ b/solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.py	
@@ -0,0 +1,16 @@
+import pandas as pd
+
+
+def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
+    filtered_orders = orders[orders["invoice"] > 20]
+    filtered_orders["month"] = (
+        filtered_orders["order_date"].dt.to_period("M").astype(str)
+    )
+    result = (
+        filtered_orders.groupby("month")
+        .agg(
+            order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
+        )
+        .reset_index()
+    )
+    return result
diff --git a/solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.sql b/solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.sql
index a33903f9fe18f..95cfaf49b7432 100644
--- a/solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.sql	
+++ b/solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.sql	
@@ -5,4 +5,4 @@ SELECT
     COUNT(DISTINCT customer_id) AS customer_count
 FROM Orders
 WHERE invoice > 20
-GROUP BY month;
+GROUP BY 1;