{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 11: Practice problems (pandas: filtering, missing values, grouping, joining)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 6, Tuesday.\n",
    "\n",
    "Fifteen problems in three levels, on the same ideas and at the same level as the lesson notebook.\n",
    "\n",
    "How to work\n",
    "\n",
    "- Write your code in the cell under each problem. The lines that are already in the cell read the input for you. Where a problem asks for a function, write it above those lines; until it is written, the cell stops with a NameError, and that is expected.\n",
    "- When the cell runs, a small box appears. Type the value there and press Enter. Several numbers on one line are typed with spaces between them.\n",
    "- Run your code with every row of the examples table and compare the output. Print exactly what the problem asks for.\n",
    "- A hint and one possible solution are under each problem. Open them only after you have tried.\n",
    "- The problems share the variables of the setup cell, so run that cell first. A problem that continues from another problem needs that problem's cell to be run before it.\n",
    "- The files orders.csv and customers.csv must be in the same folder as this notebook. On the Asiman Data School website they are already there.\n",
    "\n",
    "Tip: Say the task as a chain of small steps before you write code: \"fill the gaps, then keep the rows of Baku, then group by category, then sort\". Each step is one line. After every step, look at the result with `head()` or `shape`."
   ],
   "id": "t11p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "Run this cell first. Every problem below uses what it creates."
   ],
   "id": "t11p-001"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"orders.csv\")\n",
    "customers = pd.read_csv(\"customers.csv\")"
   ],
   "id": "t11p-002"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t11p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. The orders of Ganja\n",
    "\n",
    "Print how many orders the city Ganja has."
   ],
   "id": "t11p-004"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-005"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The condition `df[\"city\"] == \"Ganja\"` chooses the rows. `len(...)` counts them.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "print(len(df[df[\"city\"] == \"Ganja\"]))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "54\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. No rating, high price\n",
    "\n",
    "Print the rows where the rating is missing and the price is above 80."
   ],
   "id": "t11p-007"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-008"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Two masks joined with `&`: `df[\"rating\"].isna()` and `(df[\"price\"] > 80)`. The comparison needs parentheses.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "print(df[df[\"rating\"].isna() & (df[\"price\"] > 80)])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "           date customer      city  category     product   price  units  rating\n",
    "76   2025-05-02    Ramin  Sumqayit      Tech  Smartwatch  249.00      1     NaN\n",
    "80   2025-05-09    Konul      Quba  Clothing      Jacket  129.00      1     NaN\n",
    "117  2025-06-17   Ruslan     Shaki      Tech  Headphones   80.91      1     NaN\n",
    "221  2025-10-22    Aylin      Baku  Clothing    Sneakers   89.91      1     NaN\n",
    "261  2025-12-02    Ramin  Sumqayit      Tech  Headphones   89.90      1     NaN\n",
    "278  2025-12-20   Orkhan      Baku  Clothing    Sneakers   99.90      2     NaN\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. The average price of every city\n",
    "\n",
    "Print the average price of every city, rounded to 2 decimals."
   ],
   "id": "t11p-010"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-011"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Group by `city`, take the column `price`, calculate the mean: `df.groupby(\"city\")[\"price\"].mean()`. Then `.round(2)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "print(df.groupby(\"city\")[\"price\"].mean().round(2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "city\n",
    "Baku        48.84\n",
    "Ganja       54.11\n",
    "Lankaran    34.94\n",
    "Quba        49.91\n",
    "Shaki       56.50\n",
    "Sumqayit    58.64\n",
    "Name: price, dtype: float64\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. Orders with one unit\n",
    "\n",
    "Print the number of orders of every category whose product was bought with exactly 1 unit. Use `groupby` and `size`."
   ],
   "id": "t11p-013"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-014"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "First keep the rows with `df[\"units\"] == 1`, then group the result by `category` and call `.size()`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "single = df[df[\"units\"] == 1]\n",
    "print(single.groupby(\"category\").size())\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "category\n",
    "Books       36\n",
    "Clothing    36\n",
    "Home        22\n",
    "Sports      23\n",
    "Tech        52\n",
    "dtype: int64\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-015"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t11p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. One city\n",
    "\n",
    "Read the name of a city. Print how many orders this city has and their average price, rounded to 2 decimals, on one line.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Baku` | `128 48.84` |\n",
    "| `Ganja` | `54 54.11` |\n",
    "| `Shaki` | `10 56.5` |\n",
    "| `Quba` | `30 49.91` |\n",
    "| `Lankaran` | `22 34.94` |"
   ],
   "id": "t11p-017"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "Baku"
     ]
    }
   },
   "outputs": [],
   "source": [
    "city = input()\n",
    "# Write your code here"
   ],
   "id": "t11p-018"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Keep the rows of the city in a variable. Then `print(len(rows), round(rows[\"price\"].mean(), 2))`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "city = input()\n",
    "rows = df[df[\"city\"] == city]\n",
    "print(len(rows), round(rows[\"price\"].mean(), 2))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Baku` | `128 48.84` |\n",
    "| `Ganja` | `54 54.11` |\n",
    "| `Shaki` | `10 56.5` |\n",
    "| `Quba` | `30 49.91` |\n",
    "| `Lankaran` | `22 34.94` |\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. Fill the ratings\n",
    "\n",
    "Fill the missing ratings with the mean rating, rounded to 1 decimal. Print how many ratings are missing after it, and the new mean rounded to 2 decimals."
   ],
   "id": "t11p-020"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-021"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Store the rounded mean first. `df[\"rating\"] = df[\"rating\"].fillna(mean)` fills the gaps: remember to store the result. Then `isna().sum()` and the mean again.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "mean = round(df[\"rating\"].mean(), 1)\n",
    "df[\"rating\"] = df[\"rating\"].fillna(mean)\n",
    "print(df[\"rating\"].isna().sum())\n",
    "print(round(df[\"rating\"].mean(), 2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "0\n",
    "4.07\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. Books and Home between 20 and 30\n",
    "\n",
    "Print the orders of the categories Books and Home with a price between 20 and 30, with the columns `product` and `price`: only the first 5 rows."
   ],
   "id": "t11p-023"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-024"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`df[\"category\"].isin([\"Books\", \"Home\"])` and `df[\"price\"].between(20, 30)`, joined with `&`. Then choose the two columns and take `.head(5)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "chosen = df[df[\"category\"].isin([\"Books\", \"Home\"]) & df[\"price\"].between(20, 30)]\n",
    "print(chosen[[\"product\", \"price\"]].head(5))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "       product  price\n",
    "2     Cookbook  24.90\n",
    "3     Cookbook  24.90\n",
    "4   Dictionary  29.90\n",
    "16  Dictionary  29.90\n",
    "25    Cookbook  22.41\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. The money of every category\n",
    "\n",
    "Add a column `total` (price times units). Print the money of every category, the largest first, rounded to 2 decimals."
   ],
   "id": "t11p-026"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-027"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`df[\"total\"] = df[\"price\"] * df[\"units\"]`. Then group by `category`, sum the column `total`, sort with `sort_values(ascending=False)` and round.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "df[\"total\"] = df[\"price\"] * df[\"units\"]\n",
    "money = df.groupby(\"category\")[\"total\"].sum()\n",
    "print(money.sort_values(ascending=False).round(2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "category\n",
    "Tech        7424.00\n",
    "Clothing    4846.81\n",
    "Books       3629.74\n",
    "Sports      3297.39\n",
    "Home        2737.28\n",
    "Name: total, dtype: float64\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. Three summaries\n",
    "\n",
    "Print, for every category, the number of orders, the mean price and the highest price, in this order, rounded to 2 decimals."
   ],
   "id": "t11p-029"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-030"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "One `groupby` with a list of summaries: `.agg([\"count\", \"mean\", \"max\"])` on the column `price`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "print(df.groupby(\"category\")[\"price\"].agg([\"count\", \"mean\", \"max\"]).round(2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "          count   mean    max\n",
    "category                     \n",
    "Books        74  25.44   39.9\n",
    "Clothing     54  70.59  129.0\n",
    "Home         56  26.79   49.9\n",
    "Sports       48  38.99   54.9\n",
    "Tech         68  90.08  249.0\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-031"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t11p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. The busiest months\n",
    "\n",
    "Add a column `month`. Print the three months with the most orders. Use `value_counts`."
   ],
   "id": "t11p-033"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-034"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`pd.to_datetime(df[\"date\"]).dt.month` gives the month of every order. `value_counts()` puts the most frequent first, and `.head(3)` takes three.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "df[\"month\"] = pd.to_datetime(df[\"date\"]).dt.month\n",
    "print(df[\"month\"].value_counts().head(3))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "month\n",
    "12    40\n",
    "11    34\n",
    "9     28\n",
    "Name: count, dtype: int64\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. Orders with their customers\n",
    "\n",
    "Join the orders with `customers` and keep all orders: store the result in `full`. Print how many orders have no age, and the average age of the customers of each city, rounded to 1 decimal."
   ],
   "id": "t11p-036"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-037"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`full = df.merge(customers, on=\"customer\", how=\"left\")`. The gaps: `full[\"age\"].isna().sum()`. Then group `full` by `city` and take the mean of `age`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "full = df.merge(customers, on=\"customer\", how=\"left\")\n",
    "print(full[\"age\"].isna().sum())\n",
    "print(full.groupby(\"city\")[\"age\"].mean().round(1))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "3\n",
    "city\n",
    "Baku        45.6\n",
    "Ganja       37.2\n",
    "Lankaran    28.0\n",
    "Quba        30.5\n",
    "Shaki       47.3\n",
    "Sumqayit    38.7\n",
    "Name: age, dtype: float64\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. The best customer of a category\n",
    "\n",
    "**This problem continues from Problem 8**: the table already has the column `total` from your answer there. Read a category. Print the name of the customer who spent the most money in this category.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Books` | `Ziya` |\n",
    "| `Tech` | `Ramin` |\n",
    "| `Home` | `Ramin` |\n",
    "| `Clothing` | `Ramin` |\n",
    "| `Sports` | `Murad` |"
   ],
   "id": "t11p-039"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "Books"
     ]
    }
   },
   "outputs": [],
   "source": [
    "category = input()\n",
    "# Write your code here"
   ],
   "id": "t11p-040"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Keep the rows of the category, group them by `customer`, sum the column `total`. `.idxmax()` gives the name with the largest sum. If Python says there is no column `total`, finish Problem 8 first.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "category = input()\n",
    "rows = df[df[\"category\"] == category]\n",
    "spent = rows.groupby(\"customer\")[\"total\"].sum()\n",
    "print(spent.idxmax())\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Books` | `Ziya` |\n",
    "| `Tech` | `Ramin` |\n",
    "| `Home` | `Ramin` |\n",
    "| `Clothing` | `Ramin` |\n",
    "| `Sports` | `Murad` |\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. Cities and categories\n",
    "\n",
    "Print a table with the cities in the rows, the categories in the columns and the number of orders in the cells. Use `pivot_table` and count the column `price`."
   ],
   "id": "t11p-042"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-043"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`df.pivot_table(index=\"city\", columns=\"category\", values=\"price\", aggfunc=\"count\")`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "table = df.pivot_table(index=\"city\", columns=\"category\", values=\"price\", aggfunc=\"count\")\n",
    "print(table)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "category  Books  Clothing  Home  Sports  Tech\n",
    "city                                         \n",
    "Baku       29.0      28.0  21.0    25.0  25.0\n",
    "Ganja      17.0      10.0   7.0    10.0  10.0\n",
    "Lankaran    9.0       2.0   7.0     3.0   1.0\n",
    "Quba        5.0       4.0   9.0     2.0  10.0\n",
    "Shaki       3.0       2.0   NaN     2.0   3.0\n",
    "Sumqayit   11.0       5.0   9.0     5.0  18.0\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. Clean names\n",
    "\n",
    "Write a function `clean_names(column)` that removes the spaces at both ends and writes every name with a capital first letter, and returns the cleaned column."
   ],
   "id": "t11p-045"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write the function clean_names(column) here\n",
    "\n",
    "names = pd.Series([\" aylin\", \"MURAD \", \"leyla  \", \"Kamran\"])\n",
    "print(clean_names(names).tolist())"
   ],
   "id": "t11p-046"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The whole function is one line: `return column.str.strip().str.title()`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def clean_names(column):\n",
    "    return column.str.strip().str.title()\n",
    "\n",
    "names = pd.Series([\" aylin\", \"MURAD \", \"leyla  \", \"Kamran\"])\n",
    "print(clean_names(names).tolist())\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "['Aylin', 'Murad', 'Leyla', 'Kamran']\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. Money by the year of joining\n",
    "\n",
    "**This problem continues from Problem 8**: the table already has the column `total`. Join the orders with the customers (the default join is right here: an order without a customer has no year). Print the money of the customers who joined in each year, the largest first, rounded to 2 decimals."
   ],
   "id": "t11p-048"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t11p-049"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`joined = df.merge(customers, on=\"customer\")`. Then group by `joined`, sum `total`, sort with `ascending=False` and round.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "joined = df.merge(customers, on=\"customer\")\n",
    "money = joined.groupby(\"joined\")[\"total\"].sum()\n",
    "print(money.sort_values(ascending=False).round(2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "joined\n",
    "2024    6362.77\n",
    "2025    5263.75\n",
    "2023    5154.50\n",
    "2021    3596.13\n",
    "2022    1347.42\n",
    "Name: total, dtype: float64\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t11p-050"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you have finished, compare your solutions with the hidden ones. A different solution that passes every example is just as good."
   ],
   "id": "t11p-051"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
