{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": { "id": "dkPs-jZfRn8w" }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lIYdn1woOS1n", "outputId": "deab6a7d-b93d-4dad-ce34-addebf4f2167" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "House Features:\n", " [[120 3]\n", " [150 4]\n", " [ 90 2]\n", " [200 5]\n", " [110 3]]\n", "Shape: (5, 2)\n" ] } ], "source": [ "# Suppose we have 5 houses with (size in m², number of rooms)\n", "house_features = np.array([[120, 3],\n", " [150, 4],\n", " [90, 2],\n", " [200, 5],\n", " [110, 3]])\n", "\n", "print(\"House Features:\\n\", house_features)\n", "print(\"Shape:\", house_features.shape)" ] }, { "cell_type": "markdown", "metadata": { "id": "1C-Ob1g6Y99B" }, "source": [ "#Student Score Analysis\n", "uses array creation, shape, dtype, sum, and mean to analyze student performance — a typical data preprocessing task." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bzx0aWEBY-Pz", "outputId": "9182a1f5-cd09-4021-cb44-4a03dd06669b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Scores:\n", " [[85 78 92]\n", " [88 76 95]\n", " [90 82 89]\n", " [75 85 80]]\n", "Shape: (4, 3)\n", "Data Type: int64\n", "Total per student: [255 259 261 240]\n", "Average per student: [85. 86.33 87. 80. ]\n" ] } ], "source": [ "import numpy as np\n", "\n", "# Create a NumPy array for student scores in 3 subjects\n", "scores = np.array([[85, 78, 92],\n", " [88, 76, 95],\n", " [90, 82, 89],\n", " [75, 85, 80]])\n", "\n", "print(\"Scores:\\n\", scores)\n", "print(\"Shape:\", scores.shape)\n", "print(\"Data Type:\", scores.dtype)\n", "\n", "# Calculate total and average score per student\n", "total = np.sum(scores, axis=1)\n", "average = np.mean(scores, axis=1)\n", "\n", "print(\"Total per student:\", total)\n", "print(\"Average per student:\", np.round(average, 2))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Vo3vfpSZY40t" }, "source": [ "#Temperature Data Processing\n", "Shows indexing, slicing, broadcasting, and min/max functions — used in preprocessing sensor data like temperature readings." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6me28xgEJ2mm", "outputId": "29391a38-9c48-4ee3-eca8-308d281347e1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First three days: [29 31 28]\n", "Last two days: [33 27]\n", "Adjusted temperatures: [30 32 29 36 31 34 28]\n", "Max temperature: 35\n", "Min temperature: 27\n" ] } ], "source": [ "temperatures = np.array([29, 31, 28, 35, 30, 33, 27])\n", "\n", "# Indexing and slicing\n", "first_three = temperatures[:3]\n", "last_two = temperatures[-2:]\n", "\n", "# Adding 1 degree to all values (broadcasting)\n", "adjusted = temperatures + 1\n", "\n", "# Finding max and min temperature\n", "max_temp = np.max(temperatures)\n", "min_temp = np.min(temperatures)\n", "\n", "print(\"First three days:\", first_three)\n", "print(\"Last two days:\", last_two)\n", "print(\"Adjusted temperatures:\", adjusted)\n", "print(\"Max temperature:\", max_temp)\n", "print(\"Min temperature:\", min_temp)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "oU2VqSvxZRmD" }, "source": [ "#Sales Growth Calculation\n", "Combines 2D indexing, slicing, arithmetic operations, and mean calculation — practical for business or retail data analysis." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3GAIsQLTZSsj", "outputId": "5a39355c-d56b-410a-995c-cc787d769a4c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Store 2, Month 3 sales: 67\n", "Growth rate (%):\n", " [[11.11 10. ]\n", " [ 5. 6.35]]\n", "Average sales: [50. 63.33333333]\n" ] } ], "source": [ "# 2D array for monthly sales (in thousands) of two stores\n", "sales = np.array([[45, 50, 55],\n", " [60, 63, 67]])\n", "\n", "# Accessing specific element (store 2, month 3)\n", "print(\"Store 2, Month 3 sales:\", sales[1, 2])\n", "\n", "# Calculate growth rate for each store (element-wise operation)\n", "growth = (sales[:, 1:] - sales[:, :-1]) / sales[:, :-1] * 100\n", "print(\"Growth rate (%):\\n\", np.round(growth, 2))\n", "\n", "# Average sales for each store\n", "avg_sales = np.mean(sales, axis=1)\n", "print(\"Average sales:\", avg_sales)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "NqRhKq6jaaQc" }, "source": [ "#Predicting Performance using Matrix Multiplication\n", "Uses matrix multiplication (np.dot) to compute predictions — fundamental in machine learning algorithms like linear regression." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "EmjmvZblaZ9G", "outputId": "fa843346-8395-47cc-aa8d-64910609356b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Feature matrix:\n", " [[ 5 80]\n", " [ 8 90]\n", " [ 2 60]\n", " [10 95]]\n", "Weights: [2.5 0.1]\n", "Predicted performance: [20.5 29. 11. 34.5]\n" ] } ], "source": [ "# Feature matrix: [hours studied, attendance rate]\n", "X = np.array([[5, 80],\n", " [8, 90],\n", " [2, 60],\n", " [10, 95]])\n", "\n", "# Weight vector (importance of each feature)\n", "weights = np.array([2.5, 0.1])\n", "\n", "# Predicted performance using dot product\n", "predicted = np.dot(X, weights)\n", "\n", "print(\"Feature matrix:\\n\", X)\n", "print(\"Weights:\", weights)\n", "print(\"Predicted performance:\", np.round(predicted, 2))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "yY5w9UJGa2Q5" }, "source": [ "#Generating and Normalizing Random Data\n", "Demonstrates random generation, normalization, and reshaping — essential preprocessing steps for preparing data before training models." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sejN5TKHa2Bf", "outputId": "f42674f4-2b4a-40e7-b388-5f798b3d340d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original heights: [162 160 186 173 177 183 158 186 177 155]\n", "Normalized heights: [0.23 0.16 1. 0.58 0.71 0.9 0.1 1. 0.71 0. ]\n", "Reshaped heights (5x2):\n", " [[162 160]\n", " [186 173]\n", " [177 183]\n", " [158 186]\n", " [177 155]]\n" ] } ], "source": [ "# Generate random dataset (heights in cm)\n", "heights = np.random.randint(150, 190, size=10)\n", "print(\"Original heights:\", heights)\n", "\n", "# Normalization (Min-Max Scaling)\n", "normalized = (heights - np.min(heights)) / (np.max(heights) - np.min(heights))\n", "print(\"Normalized heights:\", np.round(normalized, 2))\n", "\n", "# Reshaping into 2D for further processing\n", "reshaped = heights.reshape(5, 2)\n", "print(\"Reshaped heights (5x2):\\n\", reshaped)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "bYkxcsT_dJVy" }, "source": [ "#Image Transformation Using a Square Matrix\n", "A square transformation matrix is used to adjust image intensity and contrast.\n", "Matrix operations like this are fundamental in image preprocessing and computer vision." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jsZir0kYdJEm", "outputId": "a8b8ec7c-cefa-407d-c5a8-d7014b40ac7f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[120. 144. 180.]\n", " [240. 264. 288.]\n", " [ 60. 96. 108.]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "# A 3x3 grayscale image (values 0–255)\n", "image = np.array([[100, 120, 150],\n", " [200, 220, 240],\n", " [50, 80, 90]])\n", "\n", "# Transformation matrix to increase brightness and contrast\n", "transform = np.array([[1.2, 0, 0],\n", " [0, 1.2, 0],\n", " [0, 0, 1.2]])\n", "\n", "new_image = np.dot(transform, image)\n", "print(np.round(new_image))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "QirvqjxLeLJw" }, "source": [ "#Feature Weighting Using a Diagonal Matrix\n", "A diagonal matrix is used to scale or weight features differently —\n", "a common step in machine learning preprocessing or feature importance weighting." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_WKr6BqxeLo-", "outputId": "6308d1e2-a65d-4f45-ca8e-08ec5a855fab" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 4.8 27. 0.3]\n", " [ 3. 24. 0.1]\n", " [ 5.4 28.5 0.2]]\n" ] } ], "source": [ "# Each column = feature: [Study Hours, Attendance, Projects]\n", "X = np.array([[8, 90, 3],\n", " [5, 80, 1],\n", " [9, 95, 2]])\n", "\n", "# Diagonal matrix assigning different weights\n", "weights = np.diag([0.6, 0.3, 0.1])\n", "\n", "# Weighted feature combination\n", "weighted_X = np.dot(X, weights)\n", "print(np.round(weighted_X, 2))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "wGlNSf-Je6Uu" }, "source": [ "#Solving Linear Equations Using Upper Triangular Matrix\n", "Upper triangular matrices appear naturally in Gaussian elimination.\n", "Here, they help solve systems of equations efficiently — key in engineering simulations and optimizatio" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lmnXqL-We6CN", "outputId": "99b496ab-2875-4cae-9d49-e675a866b4c1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Solution: [ 3.5 -4. 1.5]\n" ] } ], "source": [ "# Upper Triangular Matrix (from linear system after Gaussian elimination)\n", "U = np.array([[3, 2, -1],\n", " [0, 1, 4],\n", " [0, 0, 2]])\n", "b = np.array([1, 2, 3])\n", "\n", "# Solve using back-substitution\n", "x = np.linalg.solve(U, b)\n", "print(\"Solution:\", np.round(x, 2))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "-frrY7gDfZjy" }, "source": [ "#Lower Triangular Matrix in Cholesky Decomposition\n", "ower triangular matrices arise in Cholesky decomposition, used for solving systems faster in machine learning (e.g., Gaussian Processes) or finance (risk models)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HVzSDbf9fZT5", "outputId": "e63a94bd-fe72-48cb-e683-59c0db2f9ed5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lower Triangular Matrix L:\n", " [[2. 0. ]\n", " [1. 1.41]]\n" ] } ], "source": [ "# Symmetric positive-definite matrix (covariance matrix)\n", "A = np.array([[4, 2],\n", " [2, 3]])\n", "\n", "# Cholesky decomposition → A = L × Lᵀ\n", "L = np.linalg.cholesky(A)\n", "print(\"Lower Triangular Matrix L:\\n\", np.round(L, 2))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "F644LVs4f53o" }, "source": [ "#Symmetric Matrix as Covariance Matrix\n", "The covariance matrix is always symmetric\n", "used in PCA, feature correlation, and data variability analysis." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "A7wdrosHf5it", "outputId": "a16fba02-48a1-4d0c-b4f0-559fe17cc451" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Covariance (Symmetric) Matrix:\n", " [[41.67 24.17]\n", " [24.17 18.92]]\n", "Is Symmetric? True\n" ] } ], "source": [ "# Dataset: [Age, Height]\n", "data = np.array([[20, 170],\n", " [25, 175],\n", " [30, 180],\n", " [35, 178]])\n", "\n", "cov_matrix = np.cov(data.T)\n", "print(\"Covariance (Symmetric) Matrix:\\n\", np.round(cov_matrix, 2))\n", "print(\"Is Symmetric?\", np.allclose(cov_matrix, cov_matrix.T))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "2eECiaRigmDK" }, "source": [ "#Transpose in Data Alignment\n", "The transpose is used to align dimensions for matrix multiplication —\n", "fundamental in ML model training (XᵀX), least squares, and correlation computation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mHeyVUbEgmjs", "outputId": "a901dfb4-e1c0-4b5c-a613-ec63e82f9582" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Xᵀ × X:\n", " [[35 44]\n", " [44 56]]\n" ] } ], "source": [ "# 3 samples, 2 features each\n", "X = np.array([[1, 2],\n", " [3, 4],\n", " [5, 6]])\n", "\n", "# Compute correlation manually (requires Xᵀ)\n", "correlation = np.dot(X.T, X)\n", "print(\"Xᵀ × X:\\n\", correlation)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "tDxCKAp8g5nk" }, "source": [ "#Trace as a Measure of Total Variance\n", "The trace represents total variance in data (sum of variances of all features).\n", "Used in PCA to measure overall data spread and in statistical summaries." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sFE1nFMWg5VC", "outputId": "2f0bab9c-874a-41c0-f504-aa41a2ca38e0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Trace (Total Variance): 8\n" ] } ], "source": [ "# Covariance matrix\n", "cov = np.array([[5, 2],\n", " [2, 3]])\n", "\n", "trace_val = np.trace(cov)\n", "print(\"Trace (Total Variance):\", trace_val)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "KE943u6mhTYI" }, "source": [ "#Determinant and Inverse in 2D Transformations\n", "The determinant measures area scaling (how much a transformation expands or shrinks space).\n", "The inverse matrix reverses that transformation — critical in 3D graphics, robotics, and coordinate transforms." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KlaY6vSXhSoq", "outputId": "aaa819df-a9f3-419a-b418-86cb0a805240" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Determinant: 5.0\n", "Inverse Matrix:\n", " [[ 0.6 -0.2]\n", " [-0.2 0.4]]\n" ] } ], "source": [ "# Transformation matrix (rotation + scaling)\n", "T = np.array([[2, 1],\n", " [1, 3]])\n", "\n", "det = np.linalg.det(T)\n", "inv = np.linalg.inv(T)\n", "\n", "print(\"Determinant:\", round(det, 2))\n", "print(\"Inverse Matrix:\\n\", np.round(inv, 2))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "AO7wImzKh5fa" }, "source": [ "#Rank in Data Dimensionality\n", "A rank-deficient matrix means some features are linearly dependent.\n", "Low rank indicates redundant data, often detected during feature selection or PCA preprocessing." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Z8mNG9Q3h5Pv", "outputId": "934a2620-cc0d-44ed-fb32-dbbf1720defe" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix Rank: 1\n" ] } ], "source": [ "# Two features highly correlated\n", "data = np.array([[1, 2],\n", " [2, 4],\n", " [3, 6],\n", " [4, 8]])\n", "\n", "rank = np.linalg.matrix_rank(data)\n", "print(\"Matrix Rank:\", rank)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "LfpUiqJIkhzJ" }, "source": [ "#Applying Custom Functions with apply()\n", "The .apply() method lets you apply custom logic row by row — ideal for categorizing data, feature engineering, or automated labeling in ML preprocessing." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_LRjDF4FknYL", "outputId": "5730eab0-7725-4ba2-ae2e-4b0877ec3ffe" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Name Age Department Salary\n", "0 Alice 25 HR 5000\n", "1 Bob 30 IT 7000\n", "2 Charlie 35 Finance 8000\n", "3 David 40 IT 6500\n" ] } ], "source": [ "import pandas as pd\n", "data = {\n", " 'Name': ['Alice', 'Bob', 'Charlie', 'David'],\n", " 'Age': [25, 30, 35, 40],\n", " 'Department': ['HR', 'IT', 'Finance', 'IT'],\n", " 'Salary': [5000, 7000, 8000, 6500]\n", "}\n", "\n", "df = pd.DataFrame(data)\n", "print(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "v36IzVrTkhj0", "outputId": "293ed78e-4def-4dbd-de65-39fc78bf7852" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Name Age Department Salary Performance\n", "0 Alice 25 HR 5000 Low\n", "1 Bob 30 IT 7000 Medium\n", "2 Charlie 35 Finance 8000 High\n", "3 David 40 IT 6500 Medium\n" ] } ], "source": [ "# Define custom performance rating based on salary\n", "def rate_salary(s):\n", " if s > 7000:\n", " return 'High'\n", " elif s >= 6000:\n", " return 'Medium'\n", " else:\n", " return 'Low'\n", "\n", "df['Performance'] = df['Salary'].apply(rate_salary)\n", "print(df)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "9byChcWklraX" }, "source": [ " # Student Performance Analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CEiJ3cT-luA_", "outputId": "4f29b0ee-f4b1-44f7-fe8b-216bf691a3cb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 3 rows:\n", " student_id name math_score science_score attendance\n", "0 101 Alice 85.0 90.0 95\n", "1 102 Bob 92.0 88.0 88\n", "2 103 Charlie 78.0 92.0 92\n", "\n", "DataFrame shape: (6, 5)\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# Creating a Series\n", "student_ids = pd.Series([101, 102, 103, 104, 105], name='student_id')\n", "\n", "# Creating DataFrame from dictionary\n", "data = {\n", " 'student_id': [101, 102, 103, 104, 105, 106],\n", " 'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'],\n", " 'math_score': [85, 92, 78, np.nan, 88, 95],\n", " 'science_score': [90, 88, 92, 85, np.nan, 91],\n", " 'attendance': [95, 88, 92, 96, 85, 90]\n", "}\n", "df = pd.DataFrame(data)\n", "\n", "# Viewing and inspecting data\n", "print(\"First 3 rows:\")\n", "print(df.head(3))\n", "print(\"\\nDataFrame shape:\", df.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wXVnEqCpmFD8", "outputId": "6b1c9f43-9842-4a09-ec10-815e2115715c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "DataFrame Info:\n", "\n", "RangeIndex: 6 entries, 0 to 5\n", "Data columns (total 5 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 student_id 6 non-null int64 \n", " 1 name 6 non-null object \n", " 2 math_score 5 non-null float64\n", " 3 science_score 5 non-null float64\n", " 4 attendance 6 non-null int64 \n", "dtypes: float64(2), int64(2), object(1)\n", "memory usage: 372.0+ bytes\n", "None\n", "\n", "Statistical Summary:\n", " student_id math_score science_score attendance\n", "count 6.000000 5.000000 5.000000 6.000000\n", "mean 103.500000 87.600000 89.200000 91.000000\n", "std 1.870829 6.580274 2.774887 4.195235\n", "min 101.000000 78.000000 85.000000 85.000000\n", "25% 102.250000 85.000000 88.000000 88.500000\n", "50% 103.500000 88.000000 90.000000 91.000000\n", "75% 104.750000 92.000000 91.000000 94.250000\n", "max 106.000000 95.000000 92.000000 96.000000\n" ] } ], "source": [ "# Getting DataFrame information\n", "print(\"\\nDataFrame Info:\")\n", "print(df.info())\n", "\n", "# Describing statistical information\n", "print(\"\\nStatistical Summary:\")\n", "print(df.describe())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qAhcIc8ZmSZh", "outputId": "2545b60d-2786-46c4-9d38-30d68dcfb323" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Math Scores:\n", "0 85.0\n", "1 92.0\n", "2 78.0\n", "3 NaN\n", "4 88.0\n", "5 95.0\n", "Name: math_score, dtype: float64\n", "\n", "Students with >90% attendance:\n", " student_id name math_score science_score attendance\n", "0 101 Alice 85.0 90.0 95\n", "2 103 Charlie 78.0 92.0 92\n", "3 104 Diana NaN 85.0 96\n" ] } ], "source": [ "# Selecting columns\n", "math_scores = df['math_score']\n", "print(f\"\\nMath Scores:\\n{math_scores}\")\n", "\n", "# Filtering rows based on condition\n", "high_attendance = df[df['attendance'] > 90]\n", "print(f\"\\nStudents with >90% attendance:\\n{high_attendance}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "H25M7abHmSIj", "outputId": "deb69b30-6709-4017-bc93-bb3aa7a1654b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Specific selection:\n", " name math_score\n", "1 Bob 92.0\n", "2 Charlie 78.0\n", "3 Diana NaN\n", "\n", "After adding total_score:\n", " student_id name math_score science_score attendance total_score\n", "0 101 Alice 85.0 90.0 98 175.0\n", "1 102 Bob 92.0 88.0 88 180.0\n", "2 103 Charlie 78.0 92.0 92 170.0\n", "3 104 Diana NaN 85.0 96 NaN\n", "4 105 Eve 88.0 NaN 85 NaN\n", "5 106 Frank 95.0 91.0 90 186.0\n" ] } ], "source": [ "# Selecting specific rows and columns\n", "specific_data = df.loc[1:3, ['name', 'math_score']]\n", "print(f\"\\nSpecific selection:\\n{specific_data}\")\n", "\n", "# Adding a new column\n", "df['total_score'] = df['math_score'] + df['science_score']\n", "print(f\"\\nAfter adding total_score:\\n{df}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ecTKCsmanwiH", "outputId": "0fda2bae-d61c-4c42-eb80-93529cdb83f6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After updating Alice's attendance:\n", " student_id name math_score science_score attendance total_score\n", "0 101 Alice 85.0 90.0 98 175.0\n", "1 102 Bob 92.0 88.0 88 180.0\n", "2 103 Charlie 78.0 92.0 92 170.0\n", "3 104 Diana NaN 85.0 96 NaN\n", "4 105 Eve 88.0 NaN 85 NaN\n", "5 106 Frank 95.0 91.0 90 186.0\n", "\n", "After dropping student_id:\n", " name math_score science_score attendance total_score\n", "0 Alice 85.0 90.0 98 175.0\n", "1 Bob 92.0 88.0 88 180.0\n", "2 Charlie 78.0 92.0 92 170.0\n", "3 Diana NaN 85.0 96 NaN\n", "4 Eve 88.0 NaN 85 NaN\n", "5 Frank 95.0 91.0 90 186.0\n" ] } ], "source": [ "# Updating column values\n", "df.loc[df['name'] == 'Alice', 'attendance'] = 98\n", "print(f\"\\nAfter updating Alice's attendance:\\n{df}\")\n", "\n", "# Dropping columns\n", "df_clean = df.drop('student_id', axis=1)\n", "print(f\"\\nAfter dropping student_id:\\n{df_clean}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SeZIM56GmSDX", "outputId": "35f1f7ab-d4b8-41a4-8f5c-a2c81e28ce75" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Missing values:\n", "student_id 0\n", "name 0\n", "math_score 1\n", "science_score 1\n", "attendance 0\n", "total_score 2\n", "dtype: int64\n", "\n", "After filling missing values:\n", " student_id name math_score science_score attendance total_score\n", "0 101 Alice 85.0 90.0 98 175.0\n", "1 102 Bob 92.0 88.0 88 180.0\n", "2 103 Charlie 78.0 92.0 92 170.0\n", "3 104 Diana 87.6 85.0 96 NaN\n", "4 105 Eve 88.0 89.2 85 NaN\n", "5 106 Frank 95.0 91.0 90 186.0\n", "\n", "After dropping rows with missing values:\n", " student_id name math_score science_score attendance total_score\n", "0 101 Alice 85.0 90.0 98 175.0\n", "1 102 Bob 92.0 88.0 88 180.0\n", "2 103 Charlie 78.0 92.0 92 170.0\n", "5 106 Frank 95.0 91.0 90 186.0\n" ] } ], "source": [ "# Detecting missing values\n", "print(f\"\\nMissing values:\\n{df.isnull().sum()}\")\n", "\n", "# Filling missing values\n", "df_filled = df.copy()\n", "df_filled['math_score'] = df_filled['math_score'].fillna(df_filled['math_score'].mean())\n", "df_filled['science_score'] = df_filled['science_score'].fillna(df_filled['science_score'].mean())\n", "print(f\"\\nAfter filling missing values:\\n{df_filled}\")\n", "\n", "# Dropping rows with missing values\n", "df_no_missing = df.dropna()\n", "print(f\"\\nAfter dropping rows with missing values:\\n{df_no_missing}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1Qrbsu8imq7v", "outputId": "cf4fda77-730f-4bd6-ba94-f530e93eb14e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Average total score by attendance:\n", "attendance\n", "85 NaN\n", "88 180.0\n", "90 186.0\n", "92 170.0\n", "96 NaN\n", "98 175.0\n", "Name: total_score, dtype: float64\n" ] } ], "source": [ "# Grouping data and calculating mean\n", "if 'attendance' in df_filled.columns:\n", " attendance_stats = df_filled.groupby('attendance')['total_score'].mean()\n", " print(f\"\\nAverage total score by attendance:\\n{attendance_stats}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5owRAP1Bmj_3", "outputId": "48b923eb-e495-49e5-a8cd-77fb6b840816" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After applying grade calculator:\n", " student_id name math_score science_score attendance total_score \\\n", "0 101 Alice 85.0 90.0 98 175.0 \n", "1 102 Bob 92.0 88.0 88 180.0 \n", "2 103 Charlie 78.0 92.0 92 170.0 \n", "3 104 Diana 87.6 85.0 96 NaN \n", "4 105 Eve 88.0 89.2 85 NaN \n", "5 106 Frank 95.0 91.0 90 186.0 \n", "\n", " math_grade \n", "0 B \n", "1 A \n", "2 C \n", "3 B \n", "4 B \n", "5 A \n" ] } ], "source": [ "# Applying custom functions with apply()\n", "def grade_calculator(score):\n", " if score >= 90: return 'A'\n", " elif score >= 80: return 'B'\n", " elif score >= 70: return 'C'\n", " else: return 'D'\n", "\n", "df_filled['math_grade'] = df_filled['math_score'].apply(grade_calculator)\n", "print(f\"\\nAfter applying grade calculator:\\n{df_filled}\")" ] }, { "cell_type": "markdown", "metadata": { "id": "HueUME6ypxea" }, "source": [ " # Employee Performance Dashboard" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "collapsed": true, "id": "MmVQM7cjp9ak", "outputId": "52aa6732-de3e-4983-8fef-9460f23674c5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Employee Performance Dashboard\n", "\n", "First 5 rows:\n", " emp_id name department salary years_experience \\\n", "0 E001 John Doe IT 75000.0 5 \n", "1 E002 Jane Smith HR 65000.0 3 \n", "2 E003 Mike Johnson IT 80000.0 8 \n", "3 E004 Sarah Wilson Finance 70000.0 4 \n", "4 E005 Tom Brown IT NaN 2 \n", "\n", " projects_completed performance_score \n", "0 12 4.2 \n", "1 8 3.8 \n", "2 15 4.5 \n", "3 9 4.0 \n", "4 5 3.5 \n", "\n", "DataFrame shape: (6, 7)\n" ] } ], "source": [ "# Creating Series\n", "employee_ids = pd.Series(['E001', 'E002', 'E003', 'E004', 'E005'], name='emp_id')\n", "\n", "# Creating DataFrame from dictionary\n", "employee_data = {\n", " 'emp_id': ['E001', 'E002', 'E003', 'E004', 'E005', 'E006'],\n", " 'name': ['John Doe', 'Jane Smith', 'Mike Johnson', 'Sarah Wilson', 'Tom Brown', 'Lisa Davis'],\n", " 'department': ['IT', 'HR', 'IT', 'Finance', 'IT', 'HR'],\n", " 'salary': [75000, 65000, 80000, 70000, np.nan, 60000],\n", " 'years_experience': [5, 3, 8, 4, 2, 6],\n", " 'projects_completed': [12, 8, 15, 9, 5, 10],\n", " 'performance_score': [4.2, 3.8, 4.5, 4.0, 3.5, np.nan]\n", "}\n", "emp_df = pd.DataFrame(employee_data)\n", "\n", "print(\"Employee Performance Dashboard\")\n", "# Viewing and inspecting data\n", "print(f\"\\nFirst 5 rows:\\n{emp_df.head()}\")\n", "print(f\"\\nDataFrame shape: {emp_df.shape}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "pzjVqNXyqb-t", "outputId": "0b527a32-c23f-413b-88bc-6589f82442c9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "DataFrame Info:\n", "\n", "RangeIndex: 6 entries, 0 to 5\n", "Data columns (total 7 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 emp_id 6 non-null object \n", " 1 name 6 non-null object \n", " 2 department 6 non-null object \n", " 3 salary 5 non-null float64\n", " 4 years_experience 6 non-null int64 \n", " 5 projects_completed 6 non-null int64 \n", " 6 performance_score 5 non-null float64\n", "dtypes: float64(2), int64(2), object(3)\n", "memory usage: 468.0+ bytes\n", "\n", "Statistical Summary:\n", " salary years_experience projects_completed performance_score\n", "count 5.00000 6.000000 6.000000 5.000000\n", "mean 70000.00000 4.666667 9.833333 4.000000\n", "std 7905.69415 2.160247 3.430258 0.380789\n", "min 60000.00000 2.000000 5.000000 3.500000\n", "25% 65000.00000 3.250000 8.250000 3.800000\n", "50% 70000.00000 4.500000 9.500000 4.000000\n", "75% 75000.00000 5.750000 11.500000 4.200000\n", "max 80000.00000 8.000000 15.000000 4.500000\n" ] } ], "source": [ "# Getting DataFrame information\n", "print(\"\\nDataFrame Info:\")\n", "emp_df.info()\n", "\n", "# Describing statistical information\n", "print(\"\\nStatistical Summary:\")\n", "print(emp_df.describe())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Zxaxv2VJqpKw", "outputId": "2ca331e6-476c-4690-afcf-60d6679abbe5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Employee Salaries:\n", " name salary\n", "0 John Doe 75000.0\n", "1 Jane Smith 65000.0\n", "2 Mike Johnson 80000.0\n", "3 Sarah Wilson 70000.0\n", "4 Tom Brown NaN\n", "5 Lisa Davis 60000.0\n", "\n", "Employees with >4 years experience:\n", " emp_id name department salary years_experience \\\n", "0 E001 John Doe IT 75000.0 5 \n", "2 E003 Mike Johnson IT 80000.0 8 \n", "5 E006 Lisa Davis HR 60000.0 6 \n", "\n", " projects_completed performance_score \n", "0 12 4.2 \n", "2 15 4.5 \n", "5 10 NaN \n", "\n", "IT Department Employees:\n", " name salary performance_score\n", "0 John Doe 75000.0 4.2\n", "2 Mike Johnson 80000.0 4.5\n", "4 Tom Brown NaN 3.5\n" ] } ], "source": [ "# Selecting columns\n", "salaries = emp_df[['name', 'salary']]\n", "print(f\"\\nEmployee Salaries:\\n{salaries}\")\n", "\n", "# Filtering rows based on condition\n", "experienced_employees = emp_df[emp_df['years_experience'] > 4]\n", "print(f\"\\nEmployees with >4 years experience:\\n{experienced_employees}\")\n", "\n", "# Selecting specific rows and columns\n", "it_department = emp_df.loc[emp_df['department'] == 'IT', ['name', 'salary', 'performance_score']]\n", "print(f\"\\nIT Department Employees:\\n{it_department}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "X0f9NI8Lq7nx", "outputId": "fd28450d-f48c-4b56-a462-8fd292dd508f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After adding productivity column:\n", " emp_id name department salary years_experience \\\n", "0 E001 John Doe IT 75000.0 5 \n", "1 E002 Jane Smith HR 65000.0 3 \n", "2 E003 Mike Johnson IT 80000.0 8 \n", "3 E004 Sarah Wilson Finance 70000.0 4 \n", "4 E005 Tom Brown IT NaN 2 \n", "5 E006 Lisa Davis HR 60000.0 6 \n", "\n", " projects_completed performance_score productivity \n", "0 12 4.2 2.400000 \n", "1 8 3.8 2.666667 \n", "2 15 4.5 1.875000 \n", "3 9 4.0 2.250000 \n", "4 5 3.5 2.500000 \n", "5 10 NaN 1.666667 \n", "\n", "After updating John's performance score:\n", " emp_id name department salary years_experience \\\n", "0 E001 John Doe IT 75000.0 5 \n", "1 E002 Jane Smith HR 65000.0 3 \n", "2 E003 Mike Johnson IT 80000.0 8 \n", "3 E004 Sarah Wilson Finance 70000.0 4 \n", "4 E005 Tom Brown IT NaN 2 \n", "5 E006 Lisa Davis HR 60000.0 6 \n", "\n", " projects_completed performance_score productivity \n", "0 12 4.4 2.400000 \n", "1 8 3.8 2.666667 \n", "2 15 4.5 1.875000 \n", "3 9 4.0 2.250000 \n", "4 5 3.5 2.500000 \n", "5 10 NaN 1.666667 \n" ] } ], "source": [ "# Adding a new column\n", "emp_df['productivity'] = emp_df['projects_completed'] / emp_df['years_experience']\n", "print(f\"\\nAfter adding productivity column:\\n{emp_df}\")\n", "\n", "# Updating column values\n", "emp_df.loc[emp_df['name'] == 'John Doe', 'performance_score'] = 4.4\n", "print(f\"\\nAfter updating John's performance score:\\n{emp_df}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PNczN6DvrL9_", "outputId": "1286cbac-1bf9-456b-f54a-ee8c45656bd7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After dropping emp_id column:\n", " name department salary years_experience projects_completed \\\n", "0 John Doe IT 75000.0 5 12 \n", "1 Jane Smith HR 65000.0 3 8 \n", "2 Mike Johnson IT 80000.0 8 15 \n", "3 Sarah Wilson Finance 70000.0 4 9 \n", "4 Tom Brown IT 70000.0 2 5 \n", "5 Lisa Davis HR 60000.0 6 10 \n", "\n", " performance_score productivity \n", "0 4.40 2.400000 \n", "1 3.80 2.666667 \n", "2 4.50 1.875000 \n", "3 4.00 2.250000 \n", "4 3.50 2.500000 \n", "5 4.04 1.666667 \n", "\n", "Missing values:\n", "emp_id 0\n", "name 0\n", "department 0\n", "salary 0\n", "years_experience 0\n", "projects_completed 0\n", "performance_score 0\n", "productivity 0\n", "dtype: int64\n" ] } ], "source": [ "# Dropping columns\n", "emp_clean = emp_df.drop('emp_id', axis=1)\n", "print(f\"\\nAfter dropping emp_id column:\\n{emp_clean}\")\n", "\n", "# Detecting missing values\n", "print(f\"\\nMissing values:\\n{emp_df.isnull().sum()}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "QJUOjqeVrfmN", "outputId": "0e444e8e-ad3b-4d7d-e450-a0727125dc9e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After filling missing values:\n", " emp_id name department salary years_experience \\\n", "0 E001 John Doe IT 75000.0 5 \n", "1 E002 Jane Smith HR 65000.0 3 \n", "2 E003 Mike Johnson IT 80000.0 8 \n", "3 E004 Sarah Wilson Finance 70000.0 4 \n", "4 E005 Tom Brown IT 70000.0 2 \n", "5 E006 Lisa Davis HR 60000.0 6 \n", "\n", " projects_completed performance_score productivity \n", "0 12 4.40 2.400000 \n", "1 8 3.80 2.666667 \n", "2 15 4.50 1.875000 \n", "3 9 4.00 2.250000 \n", "4 5 3.50 2.500000 \n", "5 10 4.04 1.666667 \n", "\n", "After dropping any remaining missing values:\n", " emp_id name department salary years_experience \\\n", "0 E001 John Doe IT 75000.0 5 \n", "1 E002 Jane Smith HR 65000.0 3 \n", "2 E003 Mike Johnson IT 80000.0 8 \n", "3 E004 Sarah Wilson Finance 70000.0 4 \n", "4 E005 Tom Brown IT 70000.0 2 \n", "5 E006 Lisa Davis HR 60000.0 6 \n", "\n", " projects_completed performance_score productivity \n", "0 12 4.40 2.400000 \n", "1 8 3.80 2.666667 \n", "2 15 4.50 1.875000 \n", "3 9 4.00 2.250000 \n", "4 5 3.50 2.500000 \n", "5 10 4.04 1.666667 \n" ] } ], "source": [ "# Filling missing values\n", "emp_df['salary'] = emp_df['salary'].fillna(emp_df['salary'].median())\n", "emp_df['performance_score'] = emp_df['performance_score'].fillna(emp_df['performance_score'].mean())\n", "print(f\"\\nAfter filling missing values:\\n{emp_df}\")\n", "\n", "# Dropping rows with missing values (if any remain)\n", "emp_no_missing = emp_df.dropna()\n", "print(f\"\\nAfter dropping any remaining missing values:\\n{emp_no_missing}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-a13VlCarfil", "outputId": "28c317d4-5685-4c6c-a841-80ad44164ba4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Average Salary by Department:\n", "department\n", "Finance 70000.0\n", "HR 62500.0\n", "IT 75000.0\n", "Name: salary, dtype: float64\n", "\n", "Final Employee Data with Salary Categories:\n", " emp_id name department salary years_experience \\\n", "0 E001 John Doe IT 75000.0 5 \n", "1 E002 Jane Smith HR 65000.0 3 \n", "2 E003 Mike Johnson IT 80000.0 8 \n", "3 E004 Sarah Wilson Finance 70000.0 4 \n", "4 E005 Tom Brown IT 70000.0 2 \n", "5 E006 Lisa Davis HR 60000.0 6 \n", "\n", " projects_completed performance_score productivity salary_category \n", "0 12 4.40 2.400000 Medium \n", "1 8 3.80 2.666667 Low \n", "2 15 4.50 1.875000 High \n", "3 9 4.00 2.250000 Medium \n", "4 5 3.50 2.500000 Medium \n", "5 10 4.04 1.666667 Low \n" ] } ], "source": [ "# Grouping data and calculating mean\n", "dept_stats = emp_df.groupby('department')['salary'].mean()\n", "print(f\"\\nAverage Salary by Department:\\n{dept_stats}\")\n", "\n", "# Applying custom functions with apply()\n", "def salary_category(salary):\n", " if salary >= 80000: return 'High'\n", " elif salary >= 70000: return 'Medium'\n", " else: return 'Low'\n", "\n", "emp_df['salary_category'] = emp_df['salary'].apply(salary_category)\n", "print(f\"\\nFinal Employee Data with Salary Categories:\\n{emp_df}\")" ] }, { "cell_type": "markdown", "metadata": { "id": "Zn_7lnYHviNc" }, "source": [ "# Hospital Patient Management System" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "s-Uvgb_BvlFk", "outputId": "7bd8f0e3-4d9d-41fd-9bb7-f77ed0e79569" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== HOSPITAL PATIENT MANAGEMENT SYSTEM ===\n", "\n", "1. Patient Data Overview (head()):\n", " Patient_ID Patient_Name Department Age Treatment_Cost \\\n", "0 201 John Carter Cardiology 65 2500 \n", "1 202 Maria Garcia Orthopedics 42 1800 \n", "2 203 Robert Chen Neurology 58 3200 \n", "3 204 Lisa Thompson Pediatrics 12 900 \n", "4 205 James Wilson Cardiology 71 2800 \n", "\n", " Length_of_Stay Admission_Date Doctor_Rating \n", "0 5 2023-11-15 4.5 \n", "1 3 2023-12-01 4.2 \n", "2 7 2023-10-20 4.8 \n", "3 2 2024-01-05 4.9 \n", "4 6 2023-11-25 4.3 \n" ] } ], "source": [ "# Create hospital patient dataset\n", "hospital_data = {\n", " 'Patient_ID': [201, 202, 203, 204, 205, 206, 207, 208, 209, 210],\n", " 'Patient_Name': ['John Carter', 'Maria Garcia', 'Robert Chen', 'Lisa Thompson', 'James Wilson',\n", " 'Sarah Martinez', 'Michael Brown', 'Emily Davis', 'Daniel Anderson', 'Jennifer Lopez'],\n", " 'Department': ['Cardiology', 'Orthopedics', 'Neurology', 'Pediatrics', 'Cardiology',\n", " 'Orthopedics', 'Neurology', 'Pediatrics', 'Cardiology', 'Orthopedics'],\n", " 'Age': [65, 42, 58, 12, 71, 35, 62, 8, 68, 45],\n", " 'Treatment_Cost': [2500, 1800, 3200, 900, 2800, 1600, 3500, 750, 2600, 1700],\n", " 'Length_of_Stay': [5, 3, 7, 2, 6, 4, 8, 1, 5, 3],\n", " 'Admission_Date': ['2023-11-15', '2023-12-01', '2023-10-20', '2024-01-05', '2023-11-25',\n", " '2023-12-10', '2023-10-15', '2024-01-12', '2023-11-30', '2023-12-20'],\n", " 'Doctor_Rating': [4.5, 4.2, 4.8, 4.9, 4.3, 4.6, 4.7, 4.8, 4.4, 4.5]\n", "}\n", "\n", "hospital_df = pd.DataFrame(hospital_data)\n", "\n", "print(\"=== HOSPITAL PATIENT MANAGEMENT SYSTEM ===\\n\")\n", "# Viewing the first few rows\n", "print(\"1. Patient Data Overview (head()):\")\n", "print(hospital_df.head())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "G9IN1m-sv9lg", "outputId": "6a9f62cf-1d1c-42d9-acf8-d8946936ad32" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2. Patient Data Information (info()):\n", "\n", "RangeIndex: 10 entries, 0 to 9\n", "Data columns (total 8 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 Patient_ID 10 non-null int64 \n", " 1 Patient_Name 10 non-null object \n", " 2 Department 10 non-null object \n", " 3 Age 10 non-null int64 \n", " 4 Treatment_Cost 10 non-null int64 \n", " 5 Length_of_Stay 10 non-null int64 \n", " 6 Admission_Date 10 non-null object \n", " 7 Doctor_Rating 10 non-null float64\n", "dtypes: float64(1), int64(4), object(3)\n", "memory usage: 772.0+ bytes\n" ] } ], "source": [ "# Displaying basic information\n", "print(\"2. Patient Data Information (info()):\")\n", "hospital_df.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "I9W5s3DtwCzG", "outputId": "751cd7a9-f86d-48ce-b08c-36678a87b3e6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3. Patient Statistics (describe()):\n", " Age Treatment_Cost Length_of_Stay Doctor_Rating\n", "count 10.000000 10.000000 10.000000 10.000000\n", "mean 46.600000 2135.000000 4.400000 4.570000\n", "std 22.618576 932.156997 2.221111 0.231181\n", "min 8.000000 750.000000 1.000000 4.200000\n", "25% 36.750000 1625.000000 3.000000 4.425000\n", "50% 51.500000 2150.000000 4.500000 4.550000\n", "75% 64.250000 2750.000000 5.750000 4.775000\n", "max 71.000000 3500.000000 8.000000 4.900000\n" ] } ], "source": [ "# Calculating basic statistics\n", "print(\"3. Patient Statistics (describe()):\")\n", "print(hospital_df[['Age', 'Treatment_Cost', 'Length_of_Stay', 'Doctor_Rating']].describe())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oypd0wJrwH5I", "outputId": "d9a63efc-7172-46d4-c6d7-780e9d72892b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4. Cardiology Department Patients (Filtering):\n", " Patient_ID Patient_Name Department Age Treatment_Cost \\\n", "0 201 John Carter Cardiology 65 2500 \n", "4 205 James Wilson Cardiology 71 2800 \n", "8 209 Daniel Anderson Cardiology 68 2600 \n", "\n", " Length_of_Stay Admission_Date Doctor_Rating \n", "0 5 2023-11-15 4.5 \n", "4 6 2023-11-25 4.3 \n", "8 5 2023-11-30 4.4 \n" ] } ], "source": [ "# Filtering Cardiology patients\n", "print(\"4. Cardiology Department Patients (Filtering):\")\n", "cardiology_patients = hospital_df[hospital_df['Department'] == 'Cardiology']\n", "print(cardiology_patients)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2u-3U4a6wL-1", "outputId": "79130c42-8632-4261-bc92-d3200e1d5180" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5. Average Treatment Cost by Department (groupby()):\n", "Department\n", "Cardiology 2633.333333\n", "Neurology 3350.000000\n", "Orthopedics 1700.000000\n", "Pediatrics 825.000000\n", "Name: Treatment_Cost, dtype: float64\n" ] } ], "source": [ "# Average treatment cost by department\n", "print(\"5. Average Treatment Cost by Department (groupby()):\")\n", "avg_cost_by_dept = hospital_df.groupby('Department')['Treatment_Cost'].mean()\n", "print(avg_cost_by_dept)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CjJgNvA2wcwo", "outputId": "d40009eb-c6d0-4cc0-ca41-fcf5d725993e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6. Adding Daily Treatment Cost Column:\n", " Patient_Name Treatment_Cost Length_of_Stay Daily_Cost\n", "0 John Carter 2500 5 500.000000\n", "1 Maria Garcia 1800 3 600.000000\n", "2 Robert Chen 3200 7 457.142857\n", "3 Lisa Thompson 900 2 450.000000\n", "4 James Wilson 2800 6 466.666667\n", "5 Sarah Martinez 1600 4 400.000000\n", "6 Michael Brown 3500 8 437.500000\n", "7 Emily Davis 750 1 750.000000\n", "8 Daniel Anderson 2600 5 520.000000\n", "9 Jennifer Lopez 1700 3 566.666667\n" ] } ], "source": [ "# Adding daily cost column\n", "print(\"6. Adding Daily Treatment Cost Column:\")\n", "hospital_df['Daily_Cost'] = hospital_df['Treatment_Cost'] / hospital_df['Length_of_Stay']\n", "print(hospital_df[['Patient_Name', 'Treatment_Cost', 'Length_of_Stay', 'Daily_Cost']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "h3j20v3IwctF", "outputId": "907460e1-46c2-4f37-c051-7cee6f2a6ff6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7. Patients Sorted by Treatment Cost (sort_values()):\n", " Patient_Name Department Treatment_Cost\n", "6 Michael Brown Neurology 3500\n", "2 Robert Chen Neurology 3200\n", "4 James Wilson Cardiology 2800\n", "8 Daniel Anderson Cardiology 2600\n", "0 John Carter Cardiology 2500\n", "1 Maria Garcia Orthopedics 1800\n", "9 Jennifer Lopez Orthopedics 1700\n", "5 Sarah Martinez Orthopedics 1600\n", "3 Lisa Thompson Pediatrics 900\n", "7 Emily Davis Pediatrics 750\n" ] } ], "source": [ "# Sorting by treatment cost descending\n", "print(\"7. Patients Sorted by Treatment Cost (sort_values()):\")\n", "sorted_patients = hospital_df.sort_values('Treatment_Cost', ascending=False)\n", "print(sorted_patients[['Patient_Name', 'Department', 'Treatment_Cost']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cxPXaE1vwcqY", "outputId": "6bd8a3b3-d84e-4e49-ff44-d7c8e85c3621" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8. Patient Distribution by Department (value_counts()):\n", "Department\n", "Cardiology 3\n", "Orthopedics 3\n", "Neurology 2\n", "Pediatrics 2\n", "Name: count, dtype: int64\n", "\n", "==================================================\n", "\n", "9. Patients Admitted After December 2023:\n", " Patient_Name Department Admission_Date Treatment_Cost\n", "3 Lisa Thompson Pediatrics 2024-01-05 900\n", "7 Emily Davis Pediatrics 2024-01-12 750\n" ] } ], "source": [ "# Patient count by department\n", "print(\"8. Patient Distribution by Department (value_counts()):\")\n", "dept_distribution = hospital_df['Department'].value_counts()\n", "print(dept_distribution)\n", "print(\"\\n\" + \"=\"*50 + \"\\n\")\n", "# Patients admitted after December 2023\n", "print(\"9. Patients Admitted After December 2023:\")\n", "hospital_df['Admission_Date'] = pd.to_datetime(hospital_df['Admission_Date'])\n", "recent_admissions = hospital_df[hospital_df['Admission_Date'] > '2023-12-31']\n", "print(recent_admissions[['Patient_Name', 'Department', 'Admission_Date', 'Treatment_Cost']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jA4iCfXjwtE0", "outputId": "b19d291f-251f-42c1-d1e1-55f624865af3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10. Saving Recent Admissions to CSV:\n", "File 'recent_hospital_admissions.csv' created successfully!\n", "\n", "11. Additional: Average Length of Stay by Department:\n", "Department\n", "Cardiology 5.333333\n", "Neurology 7.500000\n", "Orthopedics 3.333333\n", "Pediatrics 1.500000\n", "Name: Length_of_Stay, dtype: float64\n" ] } ], "source": [ "# Saving recent admissions to CSV\n", "print(\"10. Saving Recent Admissions to CSV:\")\n", "recent_admissions.to_csv('recent_hospital_admissions.csv', index=False)\n", "print(\"File 'recent_hospital_admissions.csv' created successfully!\")\n", "\n", "# Additional analysis: Average length of stay by department\n", "print(\"\\n11. Additional: Average Length of Stay by Department:\")\n", "avg_stay_by_dept = hospital_df.groupby('Department')['Length_of_Stay'].mean()\n", "print(avg_stay_by_dept)" ] }, { "cell_type": "markdown", "metadata": { "id": "VI-_JLukz9Da" }, "source": [ "# RESTAURANT SALES & CUSTOMER ANALYTICS" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "QLJapTFC2_YN", "outputId": "38e6e02d-3b9f-4679-b90f-aadecbb30e0d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== RESTAURANT SALES & CUSTOMER ANALYTICS ===\n", "\n", "1. Restaurant Order Overview (head()):\n", " Order_ID Customer_Name Menu_Category Item_Name Order_Amount \\\n", "0 5001.0 Alice Cooper Main Course Steak 45.99 \n", "1 5002.0 Bob Marley Appetizer Bruschetta 12.50 \n", "2 5003.0 Carol King Dessert Tiramisu 8.99 \n", "3 5004.0 David Bowie Main Course Pasta 22.99 \n", "4 5005.0 Ella Fitzgerald Beverage Wine 15.75 \n", "\n", " Party_Size Service_Rating Order_Date Time_Slot Unnamed: 9 \n", "0 2.0 4.5 45306.0 Dinner NaN \n", "1 1.0 4.0 45307.0 Lunch NaN \n", "2 2.0 5.0 45308.0 Dinner NaN \n", "3 4.0 4.2 45309.0 Dinner NaN \n", "4 2.0 4.8 45310.0 Lunch NaN \n" ] } ], "source": [ "import pandas as pd\n", "# Read restaurant data from CSV file\n", "restaurant_df = pd.read_csv('restaurant_data.csv')\n", "\n", "print(\"=== RESTAURANT SALES & CUSTOMER ANALYTICS ===\\n\")\n", "\n", "# Viewing the first few rows\n", "print(\"1. Restaurant Order Overview (head()):\")\n", "print(restaurant_df.head())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "gkYJyo_y0Mfm", "outputId": "a78057df-9d43-4169-ec93-a2590f4a16c5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2. Restaurant Data Information (info()):\n", "\n", "RangeIndex: 13 entries, 0 to 12\n", "Data columns (total 10 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 Order_ID 12 non-null float64\n", " 1 Customer_Name 12 non-null object \n", " 2 Menu_Category 12 non-null object \n", " 3 Item_Name 12 non-null object \n", " 4 Order_Amount 12 non-null float64\n", " 5 Party_Size 12 non-null float64\n", " 6 Service_Rating 12 non-null float64\n", " 7 Order_Date 12 non-null float64\n", " 8 Time_Slot 12 non-null object \n", " 9 Unnamed: 9 0 non-null float64\n", "dtypes: float64(6), object(4)\n", "memory usage: 1.1+ KB\n" ] } ], "source": [ "# Displaying basic information\n", "print(\"2. Restaurant Data Information (info()):\")\n", "restaurant_df.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Bf27YoQf3AVk", "outputId": "cf7ce42e-a481-4f37-d503-f53e48ef86de" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3. Order Statistics (describe()):\n", " Order_Amount Party_Size Service_Rating\n", "count 12.000000 12.000000 12.000000\n", "mean 17.723333 1.916667 4.541667\n", "std 11.420833 0.900337 0.347611\n", "min 6.990000 1.000000 4.000000\n", "25% 10.437500 1.000000 4.275000\n", "50% 13.620000 2.000000 4.550000\n", "75% 19.990000 2.000000 4.825000\n", "max 45.990000 4.000000 5.000000\n" ] } ], "source": [ "# Calculating basic statistics\n", "print(\"3. Order Statistics (describe()):\")\n", "print(restaurant_df[['Order_Amount', 'Party_Size', 'Service_Rating']].describe())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Bk7mMAS13AI3", "outputId": "d5891a3f-6171-41dc-bbbd-072af9510b90" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4. Main Course Orders (Filtering):\n", " Order_ID Customer_Name Menu_Category Item_Name Order_Amount Party_Size \\\n", "0 5001.0 Alice Cooper Main Course Steak 45.99 2.0 \n", "3 5004.0 David Bowie Main Course Pasta 22.99 4.0 \n", "5 5006.0 Frank Sinatra Main Course Salmon 32.99 3.0 \n", "9 5010.0 Janis Joplin Main Course Burger 18.99 2.0 \n", "\n", " Service_Rating Order_Date Time_Slot Unnamed: 9 \n", "0 4.5 45306.0 Dinner NaN \n", "3 4.2 45309.0 Dinner NaN \n", "5 4.7 45311.0 Dinner NaN \n", "9 4.6 45315.0 Lunch NaN \n" ] } ], "source": [ "# Filtering Main Course orders\n", "print(\"4. Main Course Orders (Filtering):\")\n", "main_course_orders = restaurant_df[restaurant_df['Menu_Category'] == 'Main Course']\n", "print(main_course_orders)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1-aUp6J30IUm", "outputId": "56c4ff11-c170-4162-e047-4a906b6c01bc" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5. Average Order Amount by Category (groupby()):\n", "Menu_Category\n", "Appetizer 12.500000\n", "Beverage 14.370000\n", "Dessert 8.493333\n", "Main Course 30.240000\n", "Name: Order_Amount, dtype: float64\n" ] } ], "source": [ "# Average order amount by menu category\n", "print(\"5. Average Order Amount by Category (groupby()):\")\n", "avg_amount_by_category = restaurant_df.groupby('Menu_Category')['Order_Amount'].mean()\n", "print(avg_amount_by_category)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "q6zFzDPi3j-t", "outputId": "2ce283e1-3336-43c1-b078-4debaa6761c2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6. Adding Per Person Cost Column:\n", " Customer_Name Order_Amount Party_Size Per_Person_Cost\n", "0 Alice Cooper 45.99 2.0 22.995000\n", "1 Bob Marley 12.50 1.0 12.500000\n", "2 Carol King 8.99 2.0 4.495000\n", "3 David Bowie 22.99 4.0 5.747500\n", "4 Ella Fitzgerald 15.75 2.0 7.875000\n", "5 Frank Sinatra 32.99 3.0 10.996667\n", "6 Grace Jones 14.25 2.0 7.125000\n", "7 Harry Styles 9.50 1.0 9.500000\n", "8 Iggy Pop 12.99 1.0 12.990000\n", "9 Janis Joplin 18.99 2.0 9.495000\n", "10 Kurt Cobain 10.75 1.0 10.750000\n", "11 Linda Ronstadt 6.99 2.0 3.495000\n", "12 NaN NaN NaN NaN\n" ] } ], "source": [ "# Adding per person cost column\n", "print(\"6. Adding Per Person Cost Column:\")\n", "restaurant_df['Per_Person_Cost'] = restaurant_df['Order_Amount'] / restaurant_df['Party_Size']\n", "print(restaurant_df[['Customer_Name', 'Order_Amount', 'Party_Size', 'Per_Person_Cost']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_JYIkt3_3j7M", "outputId": "b5be7540-a34b-402b-ec2f-cae8d3bc195b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7. Orders Sorted by Amount (sort_values()):\n", " Customer_Name Menu_Category Item_Name Order_Amount\n", "0 Alice Cooper Main Course Steak 45.99\n", "5 Frank Sinatra Main Course Salmon 32.99\n", "3 David Bowie Main Course Pasta 22.99\n", "9 Janis Joplin Main Course Burger 18.99\n", "4 Ella Fitzgerald Beverage Wine 15.75\n", "6 Grace Jones Appetizer Calamari 14.25\n", "8 Iggy Pop Beverage Cocktail 12.99\n", "1 Bob Marley Appetizer Bruschetta 12.50\n", "10 Kurt Cobain Appetizer Salad 10.75\n", "7 Harry Styles Dessert Cheesecake 9.50\n", "2 Carol King Dessert Tiramisu 8.99\n", "11 Linda Ronstadt Dessert Ice Cream 6.99\n", "12 NaN NaN NaN NaN\n" ] } ], "source": [ "# Sorting by order amount descending\n", "print(\"7. Orders Sorted by Amount (sort_values()):\")\n", "sorted_orders = restaurant_df.sort_values('Order_Amount', ascending=False)\n", "print(sorted_orders[['Customer_Name', 'Menu_Category', 'Item_Name', 'Order_Amount']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_qodv65P3j4e", "outputId": "c43341b4-73d6-4ff2-e810-c888e43831d2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8. Order Distribution by Time Slot (value_counts()):\n", "Time_Slot\n", "Dinner 7\n", "Lunch 5\n", "Name: count, dtype: int64\n" ] } ], "source": [ "# Order count by time slot\n", "print(\"8. Order Distribution by Time Slot (value_counts()):\")\n", "time_slot_distribution = restaurant_df['Time_Slot'].value_counts()\n", "print(time_slot_distribution)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "t9zRQvgx38LW", "outputId": "dd700734-5848-4232-8a72-6fab0c4be556" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9. Converting Order Date and Filtering Recent Orders:\n", "Date conversion completed. First few dates:\n", " Order_Date\n", "0 2024-01-15\n", "1 2024-01-16\n", "2 2024-01-17\n", "3 2024-01-18\n", "4 2024-01-19\n", "\n", "Orders After January 20, 2024:\n", " Customer_Name Menu_Category Order_Date Order_Amount\n", "6 Grace Jones Appetizer 2024-01-21 14.25\n", "7 Harry Styles Dessert 2024-01-22 9.50\n", "8 Iggy Pop Beverage 2024-01-23 12.99\n", "9 Janis Joplin Main Course 2024-01-24 18.99\n", "10 Kurt Cobain Appetizer 2024-01-25 10.75\n", "11 Linda Ronstadt Dessert 2024-01-26 6.99\n" ] } ], "source": [ "# Convert Order_Date from Excel serial number to datetime\n", "print(\"9. Converting Order Date and Filtering Recent Orders:\")\n", "# The dates in CSV are Excel serial numbers (45306 = 2024-01-15)\n", "restaurant_df['Order_Date'] = pd.to_datetime(restaurant_df['Order_Date'], unit='D', origin='1899-12-30')\n", "print(\"Date conversion completed. First few dates:\")\n", "print(restaurant_df[['Order_Date']].head())\n", "\n", "# Orders after January 20, 2024\n", "recent_orders = restaurant_df[restaurant_df['Order_Date'] > '2024-01-20']\n", "print(f\"\\nOrders After January 20, 2024:\")\n", "print(recent_orders[['Customer_Name', 'Menu_Category', 'Order_Date', 'Order_Amount']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qP7DQX5v373D", "outputId": "9437abed-97a6-4b4b-ab73-211d090f8978" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10. Saving Recent Orders to CSV:\n", "File 'recent_restaurant_orders.csv' created successfully!\n", "\n", "11. Additional: Average Service Rating by Time Slot:\n", "Time_Slot\n", "Dinner 4.657143\n", "Lunch 4.380000\n", "Name: Service_Rating, dtype: float64\n" ] } ], "source": [ "# Saving recent orders to CSV\n", "print(\"10. Saving Recent Orders to CSV:\")\n", "recent_orders.to_csv('recent_restaurant_orders.csv', index=False)\n", "print(\"File 'recent_restaurant_orders.csv' created successfully!\")\n", "\n", "# Additional analysis: Average service rating by time slot\n", "print(\"\\n11. Additional: Average Service Rating by Time Slot:\")\n", "rating_by_time = restaurant_df.groupby('Time_Slot')['Service_Rating'].mean()\n", "print(rating_by_time)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CKO0tR3Z4GAP", "outputId": "992562ea-f3de-4f18-9980-e5bc4bf79be2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "12. Additional: Most Popular Menu Items:\n", "Item_Name\n", "Steak 1\n", "Bruschetta 1\n", "Tiramisu 1\n", "Pasta 1\n", "Wine 1\n", "Salmon 1\n", "Calamari 1\n", "Cheesecake 1\n", "Cocktail 1\n", "Burger 1\n", "Salad 1\n", "Ice Cream 1\n", "Name: count, dtype: int64\n", "\n", "13. Additional: Revenue Analysis by Day of Week:\n", "Day_Of_Week\n", "Friday 22.74\n", "Monday 55.49\n", "Saturday 32.99\n", "Sunday 14.25\n", "Thursday 33.74\n", "Tuesday 25.49\n", "Wednesday 27.98\n", "Name: Order_Amount, dtype: float64\n" ] } ], "source": [ "# Additional analysis: Most popular menu items\n", "print(\"\\n12. Additional: Most Popular Menu Items:\")\n", "popular_items = restaurant_df['Item_Name'].value_counts()\n", "print(popular_items)\n", "\n", "# Additional analysis: Revenue by day of week\n", "print(\"\\n13. Additional: Revenue Analysis by Day of Week:\")\n", "restaurant_df['Day_Of_Week'] = restaurant_df['Order_Date'].dt.day_name()\n", "revenue_by_day = restaurant_df.groupby('Day_Of_Week')['Order_Amount'].sum()\n", "print(revenue_by_day)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SxeLZZO-4LCV", "outputId": "a6f5d936-42fa-487d-ca11-5b25bf1db9bb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "14. Additional: Total Revenue by Menu Category:\n", "Menu_Category\n", "Appetizer 37.50\n", "Beverage 28.74\n", "Dessert 25.48\n", "Main Course 120.96\n", "Name: Order_Amount, dtype: float64\n", "\n", "15. Additional: Top Spending Customers:\n", "Customer_Name\n", "Alice Cooper 45.99\n", "Frank Sinatra 32.99\n", "David Bowie 22.99\n", "Janis Joplin 18.99\n", "Ella Fitzgerald 15.75\n", "Grace Jones 14.25\n", "Iggy Pop 12.99\n", "Bob Marley 12.50\n", "Kurt Cobain 10.75\n", "Harry Styles 9.50\n", "Carol King 8.99\n", "Linda Ronstadt 6.99\n", "Name: Order_Amount, dtype: float64\n" ] } ], "source": [ "# Additional analysis: Total revenue by menu category\n", "print(\"\\n14. Additional: Total Revenue by Menu Category:\")\n", "revenue_by_category = restaurant_df.groupby('Menu_Category')['Order_Amount'].sum()\n", "print(revenue_by_category)\n", "\n", "# Additional analysis: Customer spending patterns\n", "print(\"\\n15. Additional: Top Spending Customers:\")\n", "top_customers = restaurant_df.groupby('Customer_Name')['Order_Amount'].sum().sort_values(ascending=False)\n", "print(top_customers)" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 0 }