|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "87fd6398", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Project 1" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "ecf460cf", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "We are going to analyze the sales of real estates in Manhattan in New York from November 2024 to December. \n", |
| 17 | + "Using a rolling sales data from https://www.nyc.gov/site/finance/property/property-rolling-sales-data.page, we are going to compute the mean, median and mode of the sale price. " |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "id": "2aac3287", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "At first, we are going to download a data file. The NYC Department of Finance provides a only excel data, so you should convert it into csv file before you read the file in Python." |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 36, |
| 31 | + "id": "94ff853a", |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [ |
| 34 | + { |
| 35 | + "name": "stdout", |
| 36 | + "output_type": "stream", |
| 37 | + "text": [ |
| 38 | + "<class 'pandas.core.series.Series'>\n", |
| 39 | + "RangeIndex: 18491 entries, 0 to 18490\n", |
| 40 | + "Series name: SALE PRICE\n", |
| 41 | + "Non-Null Count Dtype \n", |
| 42 | + "-------------- ----- \n", |
| 43 | + "18491 non-null object\n", |
| 44 | + "dtypes: object(1)\n", |
| 45 | + "memory usage: 144.6+ KB\n" |
| 46 | + ] |
| 47 | + } |
| 48 | + ], |
| 49 | + "source": [ |
| 50 | + "import pandas as pd\n", |
| 51 | + "df = pd.read_csv(\"rollingsales_manhattan.csv\", skiprows= 4)\n", |
| 52 | + "\n", |
| 53 | + "# clean your data.\n", |
| 54 | + "df.columns = df.columns.str.strip()\n", |
| 55 | + "\n", |
| 56 | + "df[\"SALE PRICE\"].info()" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "id": "3d23dbd5", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "Then, we are going to compute the mean, median and mode of all prices listed. Before moving on, we should remove 0 in the list." |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": null, |
| 70 | + "id": "a8166908", |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "name": "stdout", |
| 75 | + "output_type": "stream", |
| 76 | + "text": [ |
| 77 | + "4255161.156563907\n", |
| 78 | + "1260000.0\n", |
| 79 | + "0 550000\n", |
| 80 | + "Name: SALE PRICE, dtype: int64\n" |
| 81 | + ] |
| 82 | + } |
| 83 | + ], |
| 84 | + "source": [ |
| 85 | + "# clean your data(remove \",\" in the column and change data type)\n", |
| 86 | + "df[\"SALE PRICE\"] = pd.to_numeric(df[\"SALE PRICE\"].str.replace(\",\",\"\"))\n", |
| 87 | + "\n", |
| 88 | + "# remove 0\n", |
| 89 | + "df_above_0 = df[df[\"SALE PRICE\"] > 0]\n", |
| 90 | + "\n", |
| 91 | + "average_sale_price = df_above_0[\"SALE PRICE\"].mean()\n", |
| 92 | + "median_sale_price = df_above_0[\"SALE PRICE\"].median()\n", |
| 93 | + "mode_sale_price = df_above_0[\"SALE PRICE\"].mode()\n", |
| 94 | + "\n", |
| 95 | + "print(average_sale_price)\n", |
| 96 | + "print(median_sale_price)\n", |
| 97 | + "print(mode_sale_price)" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "id": "ac8b5943", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "Let's do it again in \"hard\" way. You may not use pandas, the statistics module, a spreadsheet program, etc. You should be using the same dataset from the first step, but not accessing the DataFrame/Series.\n", |
| 106 | + "In other words, if put the code for this step in a totally separate notebook, it should still work. You should be calculating the mean, median, and mode yourself, not using functions with those names (or equivalent)." |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": null, |
| 112 | + "id": "a40a4c2f", |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [ |
| 115 | + { |
| 116 | + "name": "stdout", |
| 117 | + "output_type": "stream", |
| 118 | + "text": [ |
| 119 | + "4255161.156563907\n", |
| 120 | + "1260000\n", |
| 121 | + "550000\n" |
| 122 | + ] |
| 123 | + } |
| 124 | + ], |
| 125 | + "source": [ |
| 126 | + "import csv\n", |
| 127 | + "with open('rollingsales_manhattan.csv', mode='r',encoding=\"utf-8\") as f:\n", |
| 128 | + " reader = csv.reader(f)\n", |
| 129 | + " \n", |
| 130 | + " price_list = []\n", |
| 131 | + " price_counts = {}\n", |
| 132 | + " \n", |
| 133 | + " # skip first 5 rows\n", |
| 134 | + " for _ in range(5):\n", |
| 135 | + " next(reader)\n", |
| 136 | + " \n", |
| 137 | + " # clean each figure in the SALE PRICE column and add it to the list\n", |
| 138 | + " for row in reader:\n", |
| 139 | + " price = int(row[19].replace(\",\",\"\"))\n", |
| 140 | + " if price > 0:\n", |
| 141 | + " price_list.append(price)\n", |
| 142 | + " if price in price_counts:\n", |
| 143 | + " price_counts[price] = price_counts[price] + 1\n", |
| 144 | + " else:\n", |
| 145 | + " price_counts[price] = 1\n", |
| 146 | + " \n", |
| 147 | + " # compute the mean\n", |
| 148 | + " total_sum = 0\n", |
| 149 | + " total_count = 0\n", |
| 150 | + " for p in price_list:\n", |
| 151 | + " total_sum += p\n", |
| 152 | + " total_count += 1\n", |
| 153 | + " mean = total_sum/total_count\n", |
| 154 | + " \n", |
| 155 | + " # compute the median\n", |
| 156 | + " price_list.sort()\n", |
| 157 | + " mid_index = total_count//2\n", |
| 158 | + " if total_count % 2 == 1:\n", |
| 159 | + " median = price_list[mid_index]\n", |
| 160 | + " else:\n", |
| 161 | + " value_1 = price_list[mid_index-1]\n", |
| 162 | + " value_2 = price_list[mid_index]\n", |
| 163 | + " median = (value_1 + value_2) / 2\n", |
| 164 | + " \n", |
| 165 | + " # compute the mode\n", |
| 166 | + " max_count = 0\n", |
| 167 | + " mode_list = []\n", |
| 168 | + " for price in price_counts:\n", |
| 169 | + " if price_counts[price] > max_count:\n", |
| 170 | + " max_count = price_counts[price]\n", |
| 171 | + " mode_list = price\n", |
| 172 | + " elif price_counts[price] == max_count:\n", |
| 173 | + " mode_list.append(price)\n", |
| 174 | + " mode = mode_list\n", |
| 175 | + " print(mean)\n", |
| 176 | + " print(median)\n", |
| 177 | + " print(mode)\n", |
| 178 | + " \n", |
| 179 | + "\n", |
| 180 | + " \n", |
| 181 | + " \n" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "markdown", |
| 186 | + "id": "8c1d823c", |
| 187 | + "metadata": {}, |
| 188 | + "source": [ |
| 189 | + "Next, we are going to visualize the data we got before. Requirements is as follows:\n", |
| 190 | + "The data/calculations can come through pandas, but the drawing code should only use the Python standard library.\n", |
| 191 | + "In other words, don’t use plot(), plotly, or any other external packages.\n", |
| 192 | + "The visualization should be visual, using shape, size, symbols, etc. to represent the values. — Printing the numbers (as is) isn’t sufficient." |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": 59, |
| 198 | + "id": "e04fb443", |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [ |
| 201 | + { |
| 202 | + "name": "stdout", |
| 203 | + "output_type": "stream", |
| 204 | + "text": [ |
| 205 | + "The comparison between average and median\n", |
| 206 | + "average:##################################################################################### 4255161.156563907\n", |
| 207 | + "median:######################### 1260000.0\n" |
| 208 | + ] |
| 209 | + } |
| 210 | + ], |
| 211 | + "source": [ |
| 212 | + "scale = 50000\n", |
| 213 | + "\n", |
| 214 | + "stats_data = {\"average\": average_sale_price, \"median\":median_sale_price}\n", |
| 215 | + "\n", |
| 216 | + "print(\"The comparison between average and median\") \n", |
| 217 | + "for key in stats_data:\n", |
| 218 | + " bar_height = int(stats_data[key]/scale)\n", |
| 219 | + " bar = \"#\" * bar_height\n", |
| 220 | + " print(f\"{key}:\" f\"{bar}\", f\"{stats_data[key]}\")\n", |
| 221 | + "\n" |
| 222 | + ] |
| 223 | + } |
| 224 | + ], |
| 225 | + "metadata": { |
| 226 | + "kernelspec": { |
| 227 | + "display_name": "venv", |
| 228 | + "language": "python", |
| 229 | + "name": "python3" |
| 230 | + }, |
| 231 | + "language_info": { |
| 232 | + "codemirror_mode": { |
| 233 | + "name": "ipython", |
| 234 | + "version": 3 |
| 235 | + }, |
| 236 | + "file_extension": ".py", |
| 237 | + "mimetype": "text/x-python", |
| 238 | + "name": "python", |
| 239 | + "nbconvert_exporter": "python", |
| 240 | + "pygments_lexer": "ipython3", |
| 241 | + "version": "3.13.9" |
| 242 | + } |
| 243 | + }, |
| 244 | + "nbformat": 4, |
| 245 | + "nbformat_minor": 5 |
| 246 | +} |
0 commit comments