|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "70c1642b", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "# Data Exploration and Cleaning\n", |
| 11 | + "# Load the dataset into a Pandas DataFrame and display the first 5 rows.\n", |
| 12 | + "# Check the shape, column names, and summary statistics of the dataset.\n", |
| 13 | + "# Identify and handle missing values (fill or drop based on the data type).\n", |
| 14 | + "# Convert Transaction_Date into datetime format and extract year, month, and day as new columns." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "id": "aad7e4c2", |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "import pandas as pd\n", |
| 25 | + "import numpy as np\n", |
| 26 | + "import matplotlib.pyplot as plt\n", |
| 27 | + "import seaborn as sns\n", |
| 28 | + "\n", |
| 29 | + "# add new plot look theme\n", |
| 30 | + "sns.set_theme(style=\"whitegrid\")\n", |
| 31 | + "plt.rcParams['figure.figsize'] = (10, 6) # fixed figure size for all plots" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "id": "cb1e11c9", |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "# Load the dataset into a Pandas DataFrame and display the first 5 rows.\n", |
| 42 | + "data = pd.read_csv('credit_card_transactions.csv')\n", |
| 43 | + "print(data.head())" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "id": "08781aa3", |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "# Check the shape, column names, and summary statistics of the dataset.\n", |
| 54 | + "print(\"Shape of the dataset:\", data.shape)\n", |
| 55 | + "print(\"Column names:\", data.columns.tolist())\n", |
| 56 | + "print(\"Summary statistics:\\n\", data.describe())" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "id": "b712a3d1", |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "# Identify and handle missing values (fill or drop based on the data type).\n", |
| 67 | + "# check for missing values\n", |
| 68 | + "missing_values = data.isnull().sum()\n", |
| 69 | + "print(\"Missing values in each column:\\n\", missing_values)" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "id": "3030f2c9", |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "# check data types\n", |
| 80 | + "print(\"Data types of each column:\\n\", data.dtypes)" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": null, |
| 86 | + "id": "431c8105", |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "# change Transaction_Date to datetime\n", |
| 91 | + "data['Transaction_Date'] = pd.to_datetime(data['Transaction_Date'])" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "id": "07627be8", |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "# check data types\n", |
| 102 | + "print(\"Data types of each column:\\n\", data.dtypes)" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": null, |
| 108 | + "id": "df68cacb", |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "data.columns" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "id": "f3059fba", |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [], |
| 121 | + "source": [ |
| 122 | + "# add new columns for year, month, day\n", |
| 123 | + "data['Year'] = data['Transaction_Date'].dt.year\n", |
| 124 | + "data['Month'] = data['Transaction_Date'].dt.month\n", |
| 125 | + "data['Day'] = data['Transaction_Date'].dt.day" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "id": "cce3aa5a", |
| 132 | + "metadata": {}, |
| 133 | + "outputs": [], |
| 134 | + "source": [ |
| 135 | + "print(data[['Transaction_Date','Year','Month','Day']].head())" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": null, |
| 141 | + "id": "df2554bc", |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "# Retrieve all transactions made in January 2025.\n", |
| 146 | + "jan_2025 = data[(data['Year'] == 2025) & (data['Month'] == 1)]\n", |
| 147 | + "jan_2025.head()\n", |
| 148 | + "# export jan_2025 to csv\n", |
| 149 | + "jan_2025.to_csv('january_2025_transactions.csv', index=False)" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": 26, |
| 155 | + "id": "9b98735f", |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "# Find transactions where Amount > 700 and Transaction_Type is \"Online\".\n", |
| 160 | + "online_transactions = data[(data['Amount'] > 700) & (data['Transaction_Type'] == \"Online\")]" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "id": "5110cb6e", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "name": "stdout", |
| 171 | + "output_type": "stream", |
| 172 | + "text": [ |
| 173 | + "Number of Approved transactions: 399\n" |
| 174 | + ] |
| 175 | + } |
| 176 | + ], |
| 177 | + "source": [ |
| 178 | + "# Select only Approved transactions from the dataset.\n", |
| 179 | + "approved_transactions = data[data['Transaction_Status'] == 'Approved']\n", |
| 180 | + "# give me number of rows count where Status is Approved\n", |
| 181 | + "approved_transactions = data[data['Transaction_Status'] == 'Approved']\n", |
| 182 | + "print(\"Number of Approved transactions:\", len(approved_transactions))" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": 47, |
| 188 | + "id": "ffa96926", |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [ |
| 191 | + { |
| 192 | + "name": "stdout", |
| 193 | + "output_type": "stream", |
| 194 | + "text": [ |
| 195 | + " Amount Discounted_Amount\n", |
| 196 | + "0 360.10 360.1000\n", |
| 197 | + "1 357.02 357.0200\n", |
| 198 | + "2 829.41 787.9395\n", |
| 199 | + "3 790.35 750.8325\n", |
| 200 | + "4 311.26 311.2600\n" |
| 201 | + ] |
| 202 | + } |
| 203 | + ], |
| 204 | + "source": [ |
| 205 | + "# Create a new column Discounted_Amount, assuming a 5% discount on all transactions above 500.\n", |
| 206 | + "data['Discounted_Amount'] = data['Amount']\n", |
| 207 | + "\n", |
| 208 | + "mask = data['Amount'] > 500\n", |
| 209 | + "\n", |
| 210 | + "data.loc[mask, 'Discounted_Amount'] = data.loc[mask, 'Amount'] * 0.95\n", |
| 211 | + "\n", |
| 212 | + "print(data[['Amount', 'Discounted_Amount']].head())\n", |
| 213 | + "\n" |
| 214 | + ] |
| 215 | + } |
| 216 | + ], |
| 217 | + "metadata": { |
| 218 | + "kernelspec": { |
| 219 | + "display_name": "Python 3", |
| 220 | + "language": "python", |
| 221 | + "name": "python3" |
| 222 | + }, |
| 223 | + "language_info": { |
| 224 | + "codemirror_mode": { |
| 225 | + "name": "ipython", |
| 226 | + "version": 3 |
| 227 | + }, |
| 228 | + "file_extension": ".py", |
| 229 | + "mimetype": "text/x-python", |
| 230 | + "name": "python", |
| 231 | + "nbconvert_exporter": "python", |
| 232 | + "pygments_lexer": "ipython3", |
| 233 | + "version": "3.14.0" |
| 234 | + } |
| 235 | + }, |
| 236 | + "nbformat": 4, |
| 237 | + "nbformat_minor": 5 |
| 238 | +} |
0 commit comments