{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "To practice the provided questions in this collab, you can choose one of the following options:\n",
        "\n",
        "1. **Download the File:**\n",
        "   - Once the notebook is open, go to the \"File\" menu at the top left.\n",
        "   - Select \"Download .py\" from the dropdown menu.\n",
        "   - The notebook will be downloaded in Python script format (`.py`).\n",
        "2. **Copy and Create New Google Colab File:**\n",
        "   - Open a new Google Colab notebook.\n",
        "   - Create a new code cell by clicking the \"+\" button.\n",
        "   - Paste the copied question into the code cell.\n"
      ],
      "metadata": {
        "id": "3mYUcDm8cqz3"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# ***Python***"
      ],
      "metadata": {
        "id": "-rEtSqw-KP2V"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Functions**"
      ],
      "metadata": {
        "id": "v1_ApT5gKP2j"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- **`Functions`**: Functions are blocks of code that perform a specific task. They take input, process it, and return an output. Functions help in organizing code, promoting reusability, and making the code more modular.\n",
        "\n",
        "\n",
        "https://docs.python.org/3/tutorial/controlflow.html#defining-functions"
      ],
      "metadata": {
        "id": "F6JdTvvmKP2k"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Function to calculate the factorial of a number\n",
        "def factorial(n):\n",
        "    if n == 0 or n == 1:\n",
        "        return 1\n",
        "    else:\n",
        "        return n * factorial(n - 1)\n",
        "\n",
        "num = int(input(\"Enter a number: \"))\n",
        "result = factorial(num)\n",
        "print(\"Factorial of\", num, \"is\", result)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "71d43ab2-0ce8-4cd6-dd62-f033c5caa326",
        "id": "vnb8tSLZKP2l"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Enter a number: 5\n",
            "Factorial of 5 is 120\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Write a function that takes two arguments and returns their sum."
      ],
      "metadata": {
        "id": "WmRtu_K-KP2m"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**If-Else**"
      ],
      "metadata": {
        "id": "DA-7UGyLKP2n"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- **`If-Else Statements`**: If-else statements are used to make decisions in code. They allow you to execute different blocks of code based on whether a condition is true or false. If the condition is true, the code in the \"if\" block is executed; otherwise, the code in the \"else\" block is executed.\n",
        "\n",
        "\n",
        "\n",
        "https://docs.python.org/3/tutorial/controlflow.html#if-statements"
      ],
      "metadata": {
        "id": "vezCQAI5KP2o"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Check if a person is eligible to vote\n",
        "age = int(input(\"Enter your age: \"))\n",
        "\n",
        "if age >= 18:\n",
        "    print(\"You are eligible to vote.\")\n",
        "else:\n",
        "    print(\"You are not eligible to vote.\")\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 390
        },
        "outputId": "75a16512-30cd-47f1-a66f-56a4600ba074",
        "id": "1eee6XwNKP2o"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "KeyboardInterrupt",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-3-3fc2ea7a1266>\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# Check if a person is eligible to vote\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter your age: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mage\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0;36m18\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"You are eligible to vote.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m    849\u001b[0m                 \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    850\u001b[0m             )\n\u001b[0;32m--> 851\u001b[0;31m         return self._input_request(str(prompt),\n\u001b[0m\u001b[1;32m    852\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    853\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m    893\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    894\u001b[0m                 \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 895\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    896\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    897\u001b[0m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Write a program that checks if a given number is even or odd using if-else statements."
      ],
      "metadata": {
        "id": "QNYMxMtzKP2p"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**LOOPS**\n"
      ],
      "metadata": {
        "id": "Es0AsNToGDIg"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- **`LOOPS`**: Loops are constructs in programming that allow a specific block of code to be executed repeatedly. They are essential for automating repetitive tasks and iterating over data structures. Loops enable efficient and organized code execution, enhancing code reusability and modularity by encapsulating repetitive operations within a concise structure."
      ],
      "metadata": {
        "id": "8dyjwUJwGGwx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "# Function to calculate the sum of numbers from 1 to n using a loop\n",
        "def calculate_sum(n):\n",
        "    total = 0\n",
        "    for i in range(1, n + 1):\n",
        "        total += i\n",
        "    return total\n",
        "\n",
        "# Input from the user\n",
        "num = int(input(\"Enter a number: \"))\n",
        "\n",
        "# Call the function and print the result\n",
        "result = calculate_sum(num)\n",
        "print(\"Sum of numbers from 1 to\", num, \"is\", result)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ubr1pgC9GVny",
        "outputId": "b71453fa-68a3-4133-a501-0484ef69ed07"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Enter a number: 5\n",
            "Sum of numbers from 1 to 5 is 15\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question: Write a Python loop that prints the first 10 positive integers in ascending order.\n"
      ],
      "metadata": {
        "id": "jPZJZmbHGe9D"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Match-Case**"
      ],
      "metadata": {
        "id": "kzSlNK_HKP2p"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- **`Match-Case` :**  The `match` keyword is employed to compare an expression's value with a sequence of patterns. Using the `case` keyword followed by a specific value or pattern, the corresponding code block executes when the expression matches. If none of the cases match, the _ case is activated as the default option. This construct streamlines the decision-making process by facilitating concise pattern-based comparisons.\n",
        "\n",
        "\n",
        "https://www.geeksforgeeks.org/python-match-case-statement/"
      ],
      "metadata": {
        "id": "lQOvo4omKP2q"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Using the match statement in Python 3.10\n",
        "\n",
        "def identify_day(day_number):\n",
        "    match day_number:\n",
        "        case 1:\n",
        "            day_name = \"Sunday\"\n",
        "        case 2:\n",
        "            day_name = \"Monday\"\n",
        "        case 3:\n",
        "            day_name = \"Tuesday\"\n",
        "        case 4:\n",
        "            day_name = \"Wednesday\"\n",
        "        case 5:\n",
        "            day_name = \"Thursday\"\n",
        "        case 6:\n",
        "            day_name = \"Friday\"\n",
        "        case 7:\n",
        "            day_name = \"Saturday\"\n",
        "        case _:\n",
        "            day_name = \"Invalid day\"\n",
        "    return day_name\n",
        "\n",
        "user_input = int(input(\"Enter a day number (1-7): \"))\n",
        "print(identify_day(user_input))"
      ],
      "metadata": {
        "id": "uB28l3caKP2q"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Use match-case to determine the type of a value(integer, float or string).\n",
        "```python\n",
        "value = 42\n"
      ],
      "metadata": {
        "id": "fbNf-mnMKP2r"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "\n",
        "# ***Numpy***"
      ],
      "metadata": {
        "id": "xgJTet1dK_LC"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**1. Array Creation**"
      ],
      "metadata": {
        "id": "3JN8SMGpK_LQ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.array`: Creates a NumPy array, which is a multi-dimensional, homogeneous data structure that can hold various types of elements.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.array.html"
      ],
      "metadata": {
        "id": "C_Yje2cZK_LR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "\n",
        "# Create a NumPy array\n",
        "arr = np.array([1, 2, 3, 4, 5])\n",
        "print(\"NumPy Array:\", arr)"
      ],
      "metadata": {
        "id": "gWq5Vg0hK_LT"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Create a NumPy array containing the values [1, 2, 3, 4, 5]."
      ],
      "metadata": {
        "id": "kZsq3x0RK_LU"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.zeros`: Generates an array filled with zeros of a specified shape.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.zeros.html"
      ],
      "metadata": {
        "id": "_CkCiYrJK_LV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create an array of zeros\n",
        "zeros_arr = np.zeros((3, 4))  # 3 rows, 4 columns\n",
        "print(\"Zeros Array:\")\n",
        "print(zeros_arr)"
      ],
      "metadata": {
        "id": "CxG_RjGSK_LV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Create a 2x3 NumPy array filled with zeros."
      ],
      "metadata": {
        "id": "L8Bk0HiJK_LV"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "- `np.ones`: Creates an array filled with ones of a specified shape.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.ones.html\n"
      ],
      "metadata": {
        "id": "fgDHSS80K_LW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create an array of ones\n",
        "ones_arr = np.ones((2, 3))  # 2 rows, 3 columns\n",
        "print(\"Ones Array:\")\n",
        "print(ones_arr)"
      ],
      "metadata": {
        "id": "4SdPQOJzK_LW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Create a 3x2 NumPy array filled with ones."
      ],
      "metadata": {
        "id": "hjufGU5IK_LX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.random.rand`: Generates random numbers in a specified shape from a uniform distribution over [0, 1).\n",
        "\n",
        "https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html"
      ],
      "metadata": {
        "id": "U3ci1IAIK_LX"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "# Generate random numbers between 0 and 1\n",
        "random_arr = np.random.rand(3, 2)  # 3 rows, 2 columns\n",
        "print(\"Random Array:\")\n",
        "print(random_arr)\n"
      ],
      "metadata": {
        "id": "BfhLNqUIK_LY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Generate a 1D NumPy array with 5 random values between 0 and 1."
      ],
      "metadata": {
        "id": "nQyLGxSuK_LZ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**2. Array Operations**"
      ],
      "metadata": {
        "id": "3DmXdJBtK_LZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create two NumPy arrays\n",
        "arr1 = np.array([1, 2, 3])\n",
        "arr2 = np.array([4, 5, 6])"
      ],
      "metadata": {
        "id": "EBHZKbQxK_La"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.add`: Element-wise addition of two arrays or a scalar and an array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.add.html"
      ],
      "metadata": {
        "id": "bvrLXNXBK_La"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Perform element-wise addition\n",
        "add_result = np.add(arr1, arr2)\n",
        "print(\"Addition Result:\", add_result)\n"
      ],
      "metadata": {
        "id": "OlGWon3XK_Lb"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Add two NumPy arrays element-wise.\n",
        "```python\n",
        "array1 = np.array([16, 27, 83])\n",
        "array2 = np.array([40, 15, 26])\n"
      ],
      "metadata": {
        "id": "xVgrPiBlK_Lb"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.subtract`: Element-wise subtraction of one array from another or a scalar from an array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.subtract.html"
      ],
      "metadata": {
        "id": "dFUpTyLDK_Lb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Perform element-wise subtraction\n",
        "subtract_result = np.subtract(arr2, arr1)\n",
        "print(\"Subtraction Result:\", subtract_result)"
      ],
      "metadata": {
        "id": "tOI4x2G8K_Lc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question: Subtract one NumPy array from another element-wise.\n",
        "```python\n",
        "array1 = np.array([4, 5, 6])\n",
        "array2 = np.array([1, 2, 3])\n"
      ],
      "metadata": {
        "id": "Pv2n-ULFK_Ld"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.multiply`: Element-wise multiplication of two arrays or a scalar and an array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.multiply.html"
      ],
      "metadata": {
        "id": "gkYvDa3ZK_Ld"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Perform element-wise multiplication\n",
        "multiply_result = np.multiply(arr1, arr2)\n",
        "print(\"Multiplication Result:\", multiply_result)"
      ],
      "metadata": {
        "id": "ZChrsf0LK_Ld"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question: Multiply two NumPy arrays element-wise.\n",
        "```python\n",
        "array1 = np.array([2, 3, 4])\n",
        "array2 = np.array([5, 6, 7])\n"
      ],
      "metadata": {
        "id": "p9NqYx0LK_Le"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.divide`: Element-wise division of one array by another or a scalar by an array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.divide.html"
      ],
      "metadata": {
        "id": "5clfLp_XK_Le"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Perform element-wise division\n",
        "divide_result = np.divide(arr2, arr1)\n",
        "print(\"Division Result:\", divide_result)"
      ],
      "metadata": {
        "id": "XvHyDQPJK_Lf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Divide one NumPy array by another element-wise.\n",
        "```python\n",
        "array1 = np.array([10, 20, 30])\n",
        "array2 = np.array([2, 4, 6])\n"
      ],
      "metadata": {
        "id": "ZdLH0z6nK_Lf"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.sqrt`: Computes the element-wise square root of an array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html"
      ],
      "metadata": {
        "id": "Xgym8KZvK_Lg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Compute element-wise square root\n",
        "sqrt_result = np.sqrt(arr1)\n",
        "print(\"Square Root Result:\", sqrt_result)"
      ],
      "metadata": {
        "id": "_9R3Uzj-K_Lg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Calculate the square root of a NumPy array's elements.\n",
        "```python\n",
        "array = np.array([9, 16, 25])\n"
      ],
      "metadata": {
        "id": "ft6zcZlWK_Lh"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**3. Array Attributes**"
      ],
      "metadata": {
        "id": "0BLKNzLRK_Lh"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "\n",
        "# Create a 2D array\n",
        "arr = np.array([[1, 2, 3], [4, 5, 6]])"
      ],
      "metadata": {
        "id": "tD-o27A7K_Li"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `ndarray.shape`: Returns a tuple representing the dimensions of the array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html"
      ],
      "metadata": {
        "id": "U5DpwZQZK_Li"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Get the shape of the array\n",
        "shape = arr.shape\n",
        "print(\"Array Shape:\", shape)"
      ],
      "metadata": {
        "id": "PczDV1TuK_Li"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Get the shape of a 2D NumPy array.\n",
        "```python\n",
        "array = np.array([[1, 2, 3], [4, 5, 6]])\n"
      ],
      "metadata": {
        "id": "OAWnoE93K_Lj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `ndarray.size`: Returns the total number of elements in the array\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html"
      ],
      "metadata": {
        "id": "J4WZDdf4K_Lj"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Get the total number of elements\n",
        "size = arr.size\n",
        "print(\"Array Size:\", size)"
      ],
      "metadata": {
        "id": "TGXsqKuqK_Lj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Find the number of elements in a 1D NumPy array.\n",
        "```python\n",
        "array = np.array([10, 20, 30, 40, 50])\n"
      ],
      "metadata": {
        "id": "DbkOPuGgK_Lk"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `ndarray.ndim`: Returns the number of dimensions (axes) of the array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.ndarray.ndim.html"
      ],
      "metadata": {
        "id": "_prGtgfkK_Lk"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Get the number of dimensions\n",
        "ndim = arr.ndim\n",
        "print(\"Number of Dimensions:\", ndim)"
      ],
      "metadata": {
        "id": "TJIVIb_8K_Ll"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Determine the number of dimensions in a 3D NumPy array.\n",
        "```python\n",
        "array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\n"
      ],
      "metadata": {
        "id": "1OM41WXiK_Ll"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `ndarray.dtype`: Returns the data type of the elements in the array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.ndarray.dtype.html"
      ],
      "metadata": {
        "id": "ktNWEv-BK_Lm"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Get the data type of the elements\n",
        "dtype = arr.dtype\n",
        "print(\"Data Type:\", dtype)"
      ],
      "metadata": {
        "id": "hLf04nmBK_Lm"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Get the data type of elements in a NumPy array.\n",
        "```python\n",
        "array = np.array([1.5, 2.5, 3.5])\n"
      ],
      "metadata": {
        "id": "6k8bkcxaK_Lm"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**4. Indexing and Slicing:**"
      ],
      "metadata": {
        "id": "XvYPuxQvK_Lm"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- Indexing (`ndarray[index]`): Allows you to access a specific element in a NumPy ndarray using its index value. The index can be an integer or a tuple of integers for multidimensional arrays.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/arrays.indexing.html"
      ],
      "metadata": {
        "id": "FUDIFVGlK_Ln"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a 1D array\n",
        "arr_1d = np.array([1, 2, 3, 4, 5])\n",
        "\n",
        "# Indexing: Access a specific element\n",
        "element = arr_1d[2]  # Access the element at index 2 (3rd element)\n",
        "print(\"Indexed Element:\", element)\n",
        "\n",
        "# Create a 2D array\n",
        "arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
        "\n",
        "# Indexing: Access an element in a 2D array\n",
        "element_2d = arr_2d[1, 2]  # Access the element at row 1, column 2 (6)\n",
        "print(\"Indexed 2D Element:\", element_2d)\n",
        "\n"
      ],
      "metadata": {
        "id": "7PhbuRg_K_Ln"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Get the element at index 2 from a NumPy array.\n",
        "```python\n",
        "array = np.array([10, 20, 30, 40, 50])\n"
      ],
      "metadata": {
        "id": "gylME9U2K_Lo"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- Slicing (`ndarray[start:end]`): Allows you to extract a portion of a NumPy ndarray using a range of indices. The `start` index is inclusive, and the `end` index is exclusive. This creates a new ndarray that contains the specified slice of elements.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/arrays.indexing.html"
      ],
      "metadata": {
        "id": "SB4lp1vMK_Lo"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Slicing: Extract a portion of a 1D array\n",
        "sliced_1d = arr_1d[1:4]  # Extract elements from index 1 to 3 (exclusive)\n",
        "print(\"Sliced 1D Array:\", sliced_1d)\n",
        "\n",
        "# Slicing: Extract a portion of a 2D array\n",
        "sliced_2d = arr_2d[0:2, 1:3]  # Extract rows 0 to 1 and columns 1 to 2\n",
        "print(\"Sliced 2D Array:\")\n",
        "print(sliced_2d)"
      ],
      "metadata": {
        "id": "zzhK5zjMK_Lp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Extract elements from index 1 to 3 (inclusive) from a NumPy array.\n",
        "```python\n",
        "array = np.array([10, 20, 30, 40, 50])\n"
      ],
      "metadata": {
        "id": "Xzf77o38K_Lp"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "5. Aggregation and Statistics:"
      ],
      "metadata": {
        "id": "REhl3d5cK_Lp"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.sum`: Computes the sum of array elements along a specified axis or over the entire array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.sum.html"
      ],
      "metadata": {
        "id": "rF1Pmh0FK_Lq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a 1D array\n",
        "arr = np.array([4, 2, 9, 5, 1, 8, 6, 3, 7])\n",
        "\n",
        "# Compute the sum of array elements\n",
        "sum_result = np.sum(arr)\n",
        "print(\"Sum:\", sum_result)"
      ],
      "metadata": {
        "id": "EtfSNmwnK_Lq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Calculate the sum of elements in a NumPy array.\n",
        "```python\n",
        "array = np.array([10, 20, 30, 40, 50])\n"
      ],
      "metadata": {
        "id": "4YbOzHWJK_Lq"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.mean`: Calculates the mean (average) of array elements along a specified axis or over the entire array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.mean.html"
      ],
      "metadata": {
        "id": "UTVallOEK_Lr"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the mean of array elements\n",
        "mean_result = np.mean(arr)\n",
        "print(\"Mean:\", mean_result)"
      ],
      "metadata": {
        "id": "HnUzd6owK_Lr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Calculate the mean (average) of elements in a NumPy array.\n",
        "```python\n",
        "array = np.array([15, 25, 35, 45, 55])\n"
      ],
      "metadata": {
        "id": "Js-CFsTBK_Ls"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.median`: Computes the median (middle value) of array elements along a specified axis or over the entire array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.median.html"
      ],
      "metadata": {
        "id": "sRowBU7gK_Lt"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the median of array elements\n",
        "median_result = np.median(arr)\n",
        "print(\"Median:\", median_result)"
      ],
      "metadata": {
        "id": "cVsvU9AHK_L4"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Find the median of elements in a NumPy array.\n",
        "```python\n",
        "array = np.array([8, 10, 12, 14, 16])\n"
      ],
      "metadata": {
        "id": "Az982tHhK_L5"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.std`: Calculates the standard deviation of array elements along a specified axis or over the entire array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.std.html"
      ],
      "metadata": {
        "id": "kw7XAJ-yK_L6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the standard deviation of array elements\n",
        "std_result = np.std(arr)\n",
        "print(\"Standard Deviation:\", std_result)"
      ],
      "metadata": {
        "id": "d9UFwlDUK_L6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Calculate the standard deviation of elements in a NumPy array.\n",
        "```python\n",
        "array = np.array([5, 10, 15, 20, 25])\n"
      ],
      "metadata": {
        "id": "WjeORwveK_L7"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.min`: Finds the minimum value among array elements along a specified axis or over the entire array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.min.html"
      ],
      "metadata": {
        "id": "Lg_32M8CK_L8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Find the minimum value in the array\n",
        "min_result = np.min(arr)\n",
        "print(\"Minimum:\", min_result)"
      ],
      "metadata": {
        "id": "F0Zcsjh7K_L8"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Find the minimum value in a NumPy array.\n",
        "```python\n",
        "array = np.array([18, 12, 9, 23, 15])\n"
      ],
      "metadata": {
        "id": "YcInRNIvK_L9"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.max`: Finds the maximum value among array elements along a specified axis or over the entire array.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.max.html"
      ],
      "metadata": {
        "id": "er9Hnde8K_L9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Find the maximum value in the array\n",
        "max_result = np.max(arr)\n",
        "print(\"Maximum:\", max_result)"
      ],
      "metadata": {
        "id": "XUFY3HuRK_L-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Find the maximum value in a NumPy array.\n",
        "```python\n",
        "array = np.array([32, 45, 27, 54, 38])\n"
      ],
      "metadata": {
        "id": "pta3pbBAK_L_"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**6. Array Manipulation:**"
      ],
      "metadata": {
        "id": "5YSgoUueK_L_"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `ndarray.reshape()`: Reshapes an ndarray to a specified new shape while maintaining the original data. The total number of elements must remain unchanged.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.ndarray.reshape.html"
      ],
      "metadata": {
        "id": "0DdIOoTQK_MA"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "\n",
        "# Create a 2D array\n",
        "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
        "\n",
        "# Reshape the array to a different shape\n",
        "reshaped_arr = arr.reshape(3, 2)  # Reshaping to 3 rows, 2 columns\n",
        "print(\"Reshaped Array:\")\n",
        "print(reshaped_arr)"
      ],
      "metadata": {
        "id": "4LqAXiXBK_MA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Reshape a 1D NumPy array into a 2D array with 3 rows and 2 columns.\n",
        "```python\n",
        "array = np.array([1, 2, 3, 4, 5, 6])\n"
      ],
      "metadata": {
        "id": "pbFRXbxZK_MC"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `ndarray.transpose()`: Transposes the dimensions of an ndarray, effectively flipping rows and columns.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.ndarray.transpose.html"
      ],
      "metadata": {
        "id": "ANjvxu83K_MD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Transpose the array\n",
        "transposed_arr = arr.transpose()  # Transpose rows and columns\n",
        "print(\"Transposed Array:\")\n",
        "print(transposed_arr)"
      ],
      "metadata": {
        "id": "C_vrrXuhK_ME"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Transpose a 2D NumPy array (swap rows and columns).\n",
        "```python\n",
        "array = np.array([[1, 2, 3], [4, 5, 6]])\n"
      ],
      "metadata": {
        "id": "_9z9jPFmK_MF"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**7. Loading and Saving Data**"
      ],
      "metadata": {
        "id": "KWQ3XGBSK_MG"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.load`: Loads and returns an ndarray or a dictionary of ndarrays from a binary file (usually with a `.npy` extension) created using np.save.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.load.html"
      ],
      "metadata": {
        "id": "UU7bSoibK_MH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create an ndarray\n",
        "arr = np.array([1, 2, 3, 4, 5])\n",
        "\n",
        "# Save the ndarray to a file\n",
        "np.save('saved_array.npy', arr)\n",
        "\n",
        "\n",
        "print(\"Original Array:\", arr)"
      ],
      "metadata": {
        "id": "1pvD9ZZkK_MI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.save`: Saves an ndarray or a dictionary of ndarrays to a binary file with the `.npy` extension, preserving the data and metadata of the array(s).\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.save.html"
      ],
      "metadata": {
        "id": "sEsSgk-RK_MK"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Load the saved ndarray from the file\n",
        "loaded_arr = np.load('saved_array.npy')\n",
        "\n",
        "print(\"Loaded Array:\", loaded_arr)"
      ],
      "metadata": {
        "id": "Tds4j7ycK_ML"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**8. Handling Missing Data:**"
      ],
      "metadata": {
        "id": "3HwR70KaK_MN"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.nan`: Represents a floating-point \"Not a Number\" value, often used to indicate missing or undefined data in arrays.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/constants.html#numpy.nan"
      ],
      "metadata": {
        "id": "lmCMY292K_MO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create an array with NaN values\n",
        "arr_with_nan = np.array([1.0, np.nan, 3.0, np.nan, 5.0])"
      ],
      "metadata": {
        "id": "nNn3C1a-K_MO"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Create a NumPy array with a NaN (Not a Number) value."
      ],
      "metadata": {
        "id": "OZbF8pAqK_MP"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.isnan`: Returns a boolean array indicating which elements of an array are NaN (Not a Number).\n",
        "\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.isnan.html"
      ],
      "metadata": {
        "id": "ubVxWtCWK_MP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Check for NaN values using np.isnan\n",
        "nan_mask = np.isnan(arr_with_nan)\n",
        "print(\"NaN Mask:\", nan_mask)"
      ],
      "metadata": {
        "id": "N4Um7v7LK_MP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Check for NaN values in a NumPy array.\n",
        "```python\n",
        "array = np.array([1, 2, np.nan, 4, 5])\n"
      ],
      "metadata": {
        "id": "NWRMdxjpK_MQ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `np.where`: Returns the indices where a specified condition is met in an array, or returns values from two arrays based on a condition.\n",
        "\n",
        "https://numpy.org/doc/stable/reference/generated/numpy.where.html"
      ],
      "metadata": {
        "id": "08eCPzUfK_MQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create an array with conditions\n",
        "condition = np.array([True, False, True, False, True])\n",
        "\n",
        "# Use np.where to get indices where the condition is True\n",
        "indices = np.where(condition)\n",
        "print(\"Indices where condition is True:\", indices)\n",
        "\n",
        "# Use np.where to get values based on a condition\n",
        "values = np.where(condition, arr_with_nan, 0)\n",
        "print(\"Values based on condition:\", values)"
      ],
      "metadata": {
        "id": "m8hMhdQKK_MR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Find the indices of non-NaN values in a NumPy array.\n",
        "```python\n",
        "array = np.array([1, 2, np.nan, 4, 5])\n"
      ],
      "metadata": {
        "id": "HF4544T_K_MR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# ***Pandas***\n"
      ],
      "metadata": {
        "id": "XcqGfYwNA07G"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**1. DataFrame Creation:**"
      ],
      "metadata": {
        "id": "o-XgKjSABJKk"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**- `pd.DataFrame()`:** Creates a two-dimensional tabular data structure (DataFrame) in pandas, where data is organized in rows and columns. You can use it to manually structure and input data.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html"
      ],
      "metadata": {
        "id": "cunn8bjYBUxc"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "SPvxv_srArUF"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "\n",
        "# Creating a DataFrame from a dictionary\n",
        "data = {'Name': ['Alice', 'Bob', 'Charlie'],\n",
        "        'Age': [25, 30, 28],\n",
        "        'City': ['New York', 'Los Angeles', 'Chicago']}\n",
        "\n",
        "df = pd.DataFrame(data)\n",
        "print(df)"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "-  `pd.read_csv()` Reads data from a CSV file and constructs a DataFrame in pandas. This function automatically infers column names and data types from the file's first row, treating it as a header.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html\n"
      ],
      "metadata": {
        "id": "rYiA7nxgBpsv"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df = pd.read_csv('File name')"
      ],
      "metadata": {
        "id": "m0DRm3BBBvG0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- **Practice Question**"
      ],
      "metadata": {
        "id": "s58Ncd5AcYgj"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']\n",
        "}"
      ],
      "metadata": {
        "id": "UpAJQ0RvcsTo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Create a dataframe using above data"
      ],
      "metadata": {
        "id": "17txGnLsdFu4"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**2. Data Cleaning**"
      ],
      "metadata": {
        "id": "Rv1jSjuTCha-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data = {'Product': ['A', 'B', 'C', 'D'],\n",
        "        'Sales': [100, None, 150, 200]}\n",
        "\n",
        "df = pd.DataFrame(data)"
      ],
      "metadata": {
        "id": "HjN1NjBeE6Ap"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Table Name - Student Grades\n",
        "\n",
        "dataset = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Alice'],\n",
        "    'Math': [85, 70, 92, 64, 78],\n",
        "    'English': [90, 78, 88, 72, 85],\n",
        "    'Science': [88, 82, 95, 68, 80]\n",
        "}"
      ],
      "metadata": {
        "id": "RUDjppVIgGtI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "* `df.rename()`: Renames columns or index labels of a DataFrame.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rename.html\n",
        "\n"
      ],
      "metadata": {
        "id": "cvVaUMJwGnYs"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Rename columns\n",
        "df_renamed = df.rename(columns={'Product': 'Item', 'Sales': 'Revenue'})\n",
        "\n",
        "print(df_renamed)"
      ],
      "metadata": {
        "id": "tluSeuIFF5_R"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Practice Question :**\n",
        "Rename the columns of the \"Student Grades\" table to 'Student', 'Math Score', 'English Score', and 'Science Score'. Display the modified table."
      ],
      "metadata": {
        "id": "j17K-8YYiC47"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "* `df.replace()`: Replaces specified values in a DataFrame with new values.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html"
      ],
      "metadata": {
        "id": "GQ_7Muh1Gsbn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Replace missing values\n",
        "df_replaced = df.replace(None, 0)"
      ],
      "metadata": {
        "id": "F8PGi5RiF58x"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Practice Question :**\n",
        "Replace all occurrences of 'Alice' with 'Alicia' in the 'Name' column. Display the modified table."
      ],
      "metadata": {
        "id": "Y-SqsIr4i6D8"
      }
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "au3TzsBsh0MV"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "* `df.dropna()`: Removes rows with missing values from a DataFrame.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html"
      ],
      "metadata": {
        "id": "pXMaQKj0E_yF"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Drop rows with missing values\n",
        "df_dropped = df.dropna()"
      ],
      "metadata": {
        "id": "1oOvrLT-Fd6I"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "* `df.fillna()`: Fills missing values in a DataFrame with specified values or using specific methods.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.fillna.html\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "oM-JIrWaGyiX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Question 4:**\n",
        "Fill in the missing values in the 'Math' column with the average math score (rounded to the nearest whole number). Display the modified table.\n"
      ],
      "metadata": {
        "id": "XzVPsN24jKjH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Fill missing values with a specific value\n",
        "df_filled = df.fillna(0)"
      ],
      "metadata": {
        "id": "OM6EXYGNF56Z"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "* `df.drop_duplicates()`: Removes duplicate rows from a DataFrame.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop_duplicates.html"
      ],
      "metadata": {
        "id": "BidYvJGFG2dC"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Drop duplicate rows\n",
        "df_duplicates_removed = df.drop_duplicates()"
      ],
      "metadata": {
        "id": "HJsoagi4F5xv"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Practice Question :**\n",
        "Remove duplicate rows from the \"Student Grades\" table based on the student's name. Display the modified table."
      ],
      "metadata": {
        "id": "7R_BBgvxheOk"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "* `df.astype()`: Converts the data type of one or more columns in a DataFrame.bold text\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.astype.html"
      ],
      "metadata": {
        "id": "REVYw0aEG7KO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Convert 'Sales' column to float data type\n",
        "df_converted = df.astype({'Sales': float})"
      ],
      "metadata": {
        "id": "J1HTd96mF5ds"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.isnull()`: Returns a DataFrame of the same shape as the input, with Boolean values indicating the presence of missing values.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.isnull.html"
      ],
      "metadata": {
        "id": "3mjbCMjnHAJ-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Check for missing values\n",
        "missing_values_check = df.isnull()"
      ],
      "metadata": {
        "id": "l9k2G7dCGNN_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- **Practice questions**"
      ],
      "metadata": {
        "id": "5Qy5-nH_gBc9"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "**Question 5:**\n",
        "Create a new DataFrame that shows whether each cell in the \"Student Grades\" table is null (True) or not (False). Display the DataFrame.\n"
      ],
      "metadata": {
        "id": "C64tgPdEgH72"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**3. Data Manipulation**"
      ],
      "metadata": {
        "id": "aUE4d_UiIdiu"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data = {'Name': ['Alice', 'Bob', 'Charlie'],\n",
        "        'Age': [22, 21, 23],\n",
        "        'Grade': ['A', 'B', 'B']}\n",
        "\n",
        "df = pd.DataFrame(data)"
      ],
      "metadata": {
        "id": "CIVIAQQ8phrs"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Practice question table\n",
        "dataset = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']\n",
        "}\n"
      ],
      "metadata": {
        "id": "XWyzUo-xkZ8a"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.loc[]`: Allows you to access a group of rows and columns by labels or a boolean array along both axes.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html"
      ],
      "metadata": {
        "id": "1v-skVYCIj09"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Using df.loc[] to access rows and columns by labels\n",
        "alice_data = df.loc[0]  # Access data for Alice\n",
        "grades = df.loc[:, 'Grade']  # Access 'Grade' column\n",
        "\n",
        "print(alice_data)\n",
        "\n",
        "print(grades)"
      ],
      "metadata": {
        "id": "dIVS7MqtpKIR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : select the rows where the 'Age' column is greater than 25 from the given practice table1."
      ],
      "metadata": {
        "id": "zJJFdg3vkr-J"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.iloc[]`: Provides integer-based indexing to access rows and columns of a DataFrame.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html"
      ],
      "metadata": {
        "id": "YiDJDGajpLc_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Using df.iloc[] to access rows and columns by integer indexing\n",
        "first_row = df.iloc[0]  # Access the first row\n",
        "ages = df.iloc[:, 1]  # Access the 'Age' column\n",
        "\n",
        "print(first_row)\n",
        "print(ages)"
      ],
      "metadata": {
        "id": "wDBGFrlHpZ9G"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : select the data in the second row and third column from the given pracice table1."
      ],
      "metadata": {
        "id": "JDHl2cQpk_JA"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.apply()`: Applies a function along an axis of the DataFrame.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html"
      ],
      "metadata": {
        "id": "_aEX5mqepPwW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Using df.apply() to apply a function along an axis\n",
        "def add_years(age):\n",
        "    return age + 1\n",
        "\n",
        "df['Age_Plus_One'] = df['Age'].apply(add_years)  # Apply the function to 'Age' column\n",
        "\n",
        "print(df)"
      ],
      "metadata": {
        "id": "tZrleModpaq-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Calculate the total score (sum of Math, English, and Science) for each student in the given DataFrame.\n",
        "```python\n",
        "dataset = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Math': [85, 70, 92, 64, 78],\n",
        "    'English': [90, 78, 88, 72, 85],\n",
        "    'Science': [88, 82, 95, 68, 80]\n",
        "}\n"
      ],
      "metadata": {
        "id": "cAn_bMYYlUNp"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "- `df.map()`: Applies a function element-wise to the elements of a column or a Series.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.map.html"
      ],
      "metadata": {
        "id": "CRNI0f1QpTny"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Using df.map() to apply a function element-wise to a Series\n",
        "grade_mapping = {'A': 'Excellent', 'B': 'Good'}\n",
        "df['Grade_Description'] = df['Grade'].map(grade_mapping)  # Map grades to descriptions\n",
        "\n",
        "print(df['Grade_Description'])"
      ],
      "metadata": {
        "id": "S_CECdFmpXxB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Replace the city names with their corresponding city codes in the 'City' column of the given DataFrame.\n",
        "```python\n",
        "dataset = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']\n",
        "}\n"
      ],
      "metadata": {
        "id": "ZT1K2MwZlmZA"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**4. Sorting and Aggregating**"
      ],
      "metadata": {
        "id": "CdfMONF2Co8X"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data = {'Product': ['A', 'B', 'A', 'B', 'A'],\n",
        "        'Sales': [100, 150, 200, 120, 180],\n",
        "        'Region': ['East', 'West', 'East', 'West', 'East']}\n",
        "\n",
        "df = pd.DataFrame(data)"
      ],
      "metadata": {
        "id": "-vtZQX-Ps1KM"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# practice question table\n",
        "dataset = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']\n",
        "}\n"
      ],
      "metadata": {
        "id": "6KI4tvJemR5S"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.sort_values()`: Sorts the DataFrame rows based on specified column(s) values.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html"
      ],
      "metadata": {
        "id": "NpBAHDavsXp3"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Sort the DataFrame by 'Sales' in ascending order\n",
        "df_sorted = df.sort_values(by='Sales')\n",
        "\n",
        "print(df)"
      ],
      "metadata": {
        "id": "3ceDBo6Wsx5m"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Sort the DataFrame by the 'Age' column in ascending order."
      ],
      "metadata": {
        "id": "72DTgzdlmaOd"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.groupby()`: Groups the DataFrame rows based on unique values in one or more columns.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html"
      ],
      "metadata": {
        "id": "UgHu8uxcsdhy"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Group the DataFrame by 'Product'\n",
        "grouped = df.groupby('Product')"
      ],
      "metadata": {
        "id": "ZfRWidlcsy5k"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Group the DataFrame by the 'City' column and calculate the mean age and maximum age for each city."
      ],
      "metadata": {
        "id": "8ZqorCg1m1Fi"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `group.agg()`: Performs aggregation operations on groups created using `groupby()`.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.agg.html"
      ],
      "metadata": {
        "id": "4cbS51VQshJO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Aggregate sales data for each product group\n",
        "agg_results = grouped['Sales'].agg(['sum', 'mean'])\n",
        "\n",
        "print(agg_results)"
      ],
      "metadata": {
        "id": "1WskCPORszqr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.reset_index()`: Resets the index of the DataFrame, optionally adding a new default index.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html"
      ],
      "metadata": {
        "id": "YdydzQjqsusC"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Reset the index of the grouped DataFrame\n",
        "agg_results_reset = agg_results.reset_index()"
      ],
      "metadata": {
        "id": "9L56ai9IsXGg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Reset the index of the grouped DataFrame from Question 2 to the default index."
      ],
      "metadata": {
        "id": "qHwsShICm65q"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**5. Data Visualization**"
      ],
      "metadata": {
        "id": "Ig8bW46mu8Od"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "\n",
        "data = {'Category': ['A', 'B', 'C', 'D', 'E'],\n",
        "        'Value': [10, 20, 15, 25, 30]}\n",
        "\n",
        "df = pd.DataFrame(data)\n"
      ],
      "metadata": {
        "id": "5xT9vi8Hvgem"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.plot()`: Creates various types of plots directly from a DataFrame using the `matplotlib` library.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html"
      ],
      "metadata": {
        "id": "suJQ8p7Ku_7d"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a line plot using df.plot()\n",
        "df.plot(x='Category', y='Value', kind='line')\n",
        "plt.title('Line Plot')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "GdR4RpHKvjed"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Practice Question :** Create a line plot of the 'Age' column against the 'Name' column from the given DataFrame.\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "}\n"
      ],
      "metadata": {
        "id": "L6hJ-PgIn3sX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `plt.hist()`: Plots a histogram to visualize the distribution of data using `matplotlib`.\n",
        "\n",
        "https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html"
      ],
      "metadata": {
        "id": "S7XTQTLuvEjV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a histogram using plt.hist()\n",
        "plt.hist(df['Value'], bins=3, edgecolor='black')\n",
        "plt.title('Histogram')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "42lXU1HYv0cp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Practice Question :** Create a histogram of the 'Age' column from the given DataFrame .\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "}\n"
      ],
      "metadata": {
        "id": "ke9UIY1EoDJD"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `plt.boxplot()`: Generates a box plot to display the distribution and identify outliers using `matplotlib`.\n",
        "\n",
        "https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.boxplot.html"
      ],
      "metadata": {
        "id": "GhDGUv8RvIui"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a box plot using plt.boxplot()\n",
        "plt.boxplot(df['Value'])\n",
        "plt.title('Box Plot')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "ObmEATAtv7X0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Practice Question :** Generate a box plot of the 'Age' column from the given DataFrame.\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "}\n"
      ],
      "metadata": {
        "id": "01V0NsaCoKFu"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `plt.bar()`: Creates a bar chart to compare categorical data using `matplotlib`.\n",
        "\n",
        "https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html"
      ],
      "metadata": {
        "id": "OJfmlI51vNFZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a bar chart using plt.bar()\n",
        "plt.bar(df['Category'], df['Value'])\n",
        "plt.title('Bar Chart')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "nNt69TMKwEAR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Practice Question :** Create a bar plot of the 'Name' column against the 'Age' column using the given DataFrame.\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "}\n"
      ],
      "metadata": {
        "id": "8-g2vLNgoW4s"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `plt.scatter()`: Produces a scatter plot to show the relationship between two numerical variables using `matplotlib`\n",
        "\n",
        "https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html"
      ],
      "metadata": {
        "id": "GXqLrxc9vQN7"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a scatter plot using plt.scatter()\n",
        "plt.scatter(df['Category'], df['Value'])\n",
        "plt.title('Scatter Plot')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "sha4XYJbwKRu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Generate a scatter plot of the 'Math' column against the 'Science' column from the given DataFrame .\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Math': [85, 70, 92, 64, 78],\n",
        "    'Science': [88, 82, 95, 68, 80]\n",
        "}\n"
      ],
      "metadata": {
        "id": "JgBOg58vozW3"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `sns.barplot()`: Generates a bar plot using the `seaborn` library, often used for statistical estimation.\n",
        "\n",
        "https://seaborn.pydata.org/generated/seaborn.barplot.html"
      ],
      "metadata": {
        "id": "j4mqmBv0vTNn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a bar plot using sns.barplot()\n",
        "sns.barplot(x='Category', y='Value', data=df)\n",
        "plt.title('Seaborn Bar Plot')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "e1nsy15PwRRw"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Use Seaborn's `sns.barplot()` to create a bar plot of the average 'Age' for each 'Name' from the given DataFrame.\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "}\n"
      ],
      "metadata": {
        "id": "QYAEakP0o9qo"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `sns.lineplot()`: Creates a line plot using the `seaborn` library to visualize trends or relationships in data.\n",
        "\n",
        "https://seaborn.pydata.org/generated/seaborn.lineplot.html"
      ],
      "metadata": {
        "id": "PrP3xwmZvV7R"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a line plot using sns.lineplot()\n",
        "sns.lineplot(x='Category', y='Value', data=df)\n",
        "plt.title('Seaborn Line Plot')\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "aAutTyq9vbJg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Generate a line plot of the 'Age' column against the 'Name' column  from the given DataFrame.\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emily'],\n",
        "    'Age': [25, 30, 22, 28, 24],\n",
        "}\n"
      ],
      "metadata": {
        "id": "CB9DVKGYpH_m"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**6. Data Transformation**"
      ],
      "metadata": {
        "id": "jZdulwiP15Jo"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data = {'Product': ['A', 'A', 'B', 'B'],\n",
        "        'Region': ['East', 'West', 'East', 'West'],\n",
        "        'Sales': [100, 150, 200, 120]}\n",
        "\n",
        "df = pd.DataFrame(data)"
      ],
      "metadata": {
        "id": "3sOo6-532q2C"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `pd.pivot_table()`: Creates a pivot table from a DataFrame, allowing you to summarize and aggregate data based on multiple dimensions.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html"
      ],
      "metadata": {
        "id": "VbYZ2sTg2Ppq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a pivot table to summarize sales by product and region\n",
        "pivot_table = pd.pivot_table(df, values='Sales', index='Product', columns='Region', aggfunc='sum')\n",
        "\n",
        "print(\"Pivot Table:\")\n",
        "print(pivot_table)"
      ],
      "metadata": {
        "id": "RcAFmyHA2jnA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Create a pivot table from the given DataFrame that shows the average 'Score' for each 'Subject' and 'Student'.\n",
        "```python\n",
        "data = {\n",
        "    'Student': ['Alice', 'Bob', 'Alice', 'Bob', 'Alice', 'Bob'],\n",
        "    'Subject': ['Math', 'Math', 'English', 'English', 'Science', 'Science'],\n",
        "    'Score': [85, 70, 90, 78, 88, 82]\n",
        "}\n"
      ],
      "metadata": {
        "id": "MKwVRTpSpkOx"
      }
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "BqJklrSRpkHb"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.stack()`: Reshapes the DataFrame by \"stacking\" the columns to create a multi-level index.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.stack.html"
      ],
      "metadata": {
        "id": "9h37T3HJ2RTu"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Stack the DataFrame to create a multi-level index\n",
        "stacked_df = df.set_index(['Product', 'Region']).stack()\n",
        "\n",
        "print(\"\\nStacked DataFrame:\")\n",
        "print(stacked_df)"
      ],
      "metadata": {
        "id": "sieG2l852kRC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Transform the given DataFrame into a stacked format.\n",
        "```python\n",
        "data = {\n",
        "    'Student': ['Alice', 'Bob'],\n",
        "    'Math': [85, 70],\n",
        "    'English': [90, 78],\n",
        "    'Science': [88, 82]\n",
        "}\n"
      ],
      "metadata": {
        "id": "tNG1cEfipwHs"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "FE6Dl4RXpvpU"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.unstack()`: Reshapes a multi-level index DataFrame by \"unstacking\" one level of the index to create columns.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.unstack.html"
      ],
      "metadata": {
        "id": "M1ZUehOj2VSc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Unstack the DataFrame to reshape it back\n",
        "unstacked_df = stacked_df.unstack()\n",
        "\n",
        "print(\"\\nUnstacked DataFrame:\")\n",
        "print(unstacked_df)"
      ],
      "metadata": {
        "id": "vsYLqaEM2lCw"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Unstack the 'Subject' index level of the pivot table from Question 1.\n",
        "```python\n",
        "data = {\n",
        "    'Student': ['Alice', 'Bob'],\n",
        "    'Math': [85, 70],\n",
        "    'English': [90, 78],\n",
        "    'Science': [88, 82]\n",
        "}\n"
      ],
      "metadata": {
        "id": "Jo2k9oKNp8EY"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**7. Handling DateTime Data**"
      ],
      "metadata": {
        "id": "y1ofKsuO2EaY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "\n",
        "data = {'Date': ['2023-01-15', '2023-03-20', '2023-06-10']}\n",
        "df = pd.DataFrame(data)"
      ],
      "metadata": {
        "id": "JYSzQR6p2pdk"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `pd.to_datetime()`: Converts a column or array of date-like values into datetime objects.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html"
      ],
      "metadata": {
        "id": "QzZnzjR62MWw"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Convert the 'Date' column to datetime objects\n",
        "df['Date'] = pd.to_datetime(df['Date'])\n",
        "\n",
        "print(df['Date'])"
      ],
      "metadata": {
        "id": "MYvzXg7f2izV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Convert the 'Date' column in the given DataFrame to datetime format .\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol'],\n",
        "    'Date': ['2022-03-15', '2021-09-10', '2023-01-25']\n",
        "}\n"
      ],
      "metadata": {
        "id": "wwdLtf-RqYBl"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.dt.year`: Extracts the year component from a datetime-like column in a DataFrame.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.year.html"
      ],
      "metadata": {
        "id": "FBu7Wzf22aTg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Extract the year components\n",
        "df['Year'] = df['Date'].dt.year\n",
        "\n",
        "print(df['Year'])"
      ],
      "metadata": {
        "id": "cYc5uxiw2h24"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Extract the year from the 'Date' column in the given DataFrame .\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol'],\n",
        "    'Date': ['2022-03-15', '2021-09-10', '2023-01-25']\n",
        "}\n"
      ],
      "metadata": {
        "id": "ATBI-H5PqcvW"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `df.dt.month`: Extracts the month component from a datetime-like column in a DataFrame.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.month.html"
      ],
      "metadata": {
        "id": "MYW8m7us2eAk"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Extract the  month components\n",
        "\n",
        "df['Month'] = df['Date'].dt.month\n",
        "\n",
        "print(df['Month'])"
      ],
      "metadata": {
        "id": "81a0WF682g3s"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Extract the month from the 'Date' column in the given DataFrame .\n",
        "```python\n",
        "data = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol'],\n",
        "    'Date': ['2022-03-15', '2021-09-10', '2023-01-25']\n",
        "}\n"
      ],
      "metadata": {
        "id": "tJ4whE63qvgb"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**8. Merging Data**"
      ],
      "metadata": {
        "id": "9kco2qB33DPN"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `pd.merge()`: Combines two or more DataFrames based on common columns or indices using various types of joins, similar to SQL join operations.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge.html"
      ],
      "metadata": {
        "id": "CJ8UDiLZ3OFG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data1 = {'ID': [1, 2, 3],\n",
        "         'Name': ['Alice', 'Bob', 'Charlie']}\n",
        "df1 = pd.DataFrame(data1)\n",
        "\n",
        "data2 = {'ID': [1, 2, 4],\n",
        "         'Age': [25, 30, 28]}\n",
        "df2 = pd.DataFrame(data2)\n",
        "\n",
        "# Merge the DataFrames based on the 'ID' column using an inner join\n",
        "merged_df = pd.merge(df1, df2, on='ID', how='inner')\n",
        "\n",
        "print(merged_df)\n"
      ],
      "metadata": {
        "id": "PEFwDr5V3W5_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Perform an inner merge between the two given DataFrames ('left_df' and 'right_df') on the 'ID' column.\n",
        "```python\n",
        "left_data = {\n",
        "    'ID': [1, 2, 3],\n",
        "    'Name': ['Alice', 'Bob', 'Carol']\n",
        "}\n",
        "\n",
        "right_data = {\n",
        "    'ID': [2, 3, 4],\n",
        "    'Age': [25, 30, 22]\n",
        "}\n"
      ],
      "metadata": {
        "id": "9sV058ZzrQ2I"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**9. Handling Large Datasets**"
      ],
      "metadata": {
        "id": "gIOsF9MX4v4p"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- `pd.concat()`: Concatenates (joins) multiple DataFrames along a specified axis, either row-wise or column-wise, allowing you to combine data from different sources or manipulate dataframes together.\n",
        "\n",
        "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html"
      ],
      "metadata": {
        "id": "hlgwTIy640ZB"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "data1 = {'ID': [1, 2, 3],\n",
        "         'Name': ['Alice', 'Bob', 'Charlie']}\n",
        "df1 = pd.DataFrame(data1)\n",
        "\n",
        "data2 = {'ID': [4, 5, 6],\n",
        "         'Name': ['David', 'Eve', 'Frank']}\n",
        "df2 = pd.DataFrame(data2)\n",
        "\n",
        "# Concatenate the DataFrames row-wise\n",
        "concatenated_df = pd.concat([df1, df2], ignore_index=True)\n",
        "\n",
        "print(concatenated_df)\n"
      ],
      "metadata": {
        "id": "eXnkDFJF4_Kx"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Practice Question : Concatenate the two given DataFrames ('df1' and 'df2') vertically.\n",
        "```python\n",
        "data1 = {\n",
        "    'Name': ['Alice', 'Bob', 'Carol'],\n",
        "    'Age': [25, 30, 22]\n",
        "}\n",
        "\n",
        "data2 = {\n",
        "    'Name': ['David', 'Emily'],\n",
        "    'Age': [28, 24]\n",
        "}\n"
      ],
      "metadata": {
        "id": "vBFodTUZrV9B"
      }
    }
  ]
}