├── 01-Python-Crash-Course ├── Python Crash Course Exercises - Solutions.ipynb ├── Python Crash Course Exercises .ipynb └── Python Crash Course.ipynb ├── 02-NumPy ├── 1-M-NumPy-Arrays.ipynb ├── 2-M-Numpy-Indexing-and-Selection.ipynb ├── 3-M-Numpy-Operations.ipynb ├── M-Numpy Exercise - Solutions.ipynb └── d-Numpy Exercises.ipynb ├── 03- General Pandas ├── 01-d-Introduction-to-Pandas.ipynb ├── 02-M-Series.ipynb ├── 03-M-DataFrames.ipynb ├── 04-M-Missing-Data.ipynb ├── 05-M-Groupby.ipynb ├── 06-M-Merging-Joining-and-Concatenating.ipynb ├── 07-M-Operations.ipynb ├── 08-M-Data-Input-and-Output.ipynb ├── Excel_Sample.xlsx ├── Pandas-Exercises │ ├── Pandas Exercise SOLUTIONS.ipynb │ ├── Pandas Exercises.ipynb │ └── banklist.csv ├── example └── multi_index_example ├── 04-Visualization-Matplotlib-Pandas ├── 04-01-Matplotlib │ ├── 01-M-Matplotlib Concepts Lecture.ipynb │ ├── 02-M-(Optional-No Video) - Advanced Matplotlib Concepts.ipynb │ ├── 03-M-Matplotlib Exercises - Solutions.ipynb │ ├── Matplotlib Exercises .ipynb │ └── filename.png └── 04-02-Pandas Visualization │ ├── 01-d-Pandas Built-in Data Visualization.ipynb │ ├── 02-d-Visualizing Time Series Data.ipynb │ ├── 03-M-Pandas Data Visualization Exercise - Solutions.ipynb │ ├── 04-M-Pandas Data Visualization Exercise .ipynb │ ├── df1 │ ├── df2 │ ├── df3 │ └── mcdonalds.csv ├── 05-Pandas-with-Time-Series ├── 01-M-Datetime Index.ipynb ├── 02-M-Rolling and Expanding.ipynb ├── 03-M-Time Resampling.ipynb ├── 04-M-Time Shifting.ipynb └── time_data │ └── walmart_stock.csv ├── 06-Data-Sources ├── 2-M-Quandl.ipynb └── Doesn't_work_1-Pandas-Datareader.ipynb ├── 07-Stock-Market-Analysis-Capstone-Project ├── 01-M-Stock Market Analysis Project SOLUTIONS.ipynb ├── 02-d-Stock Market Analysis Project.ipynb ├── Ford_Stock.csv ├── GM_Stock.csv └── Tesla_Stock.csv ├── 08-Time-Series-Analysis ├── 1-Introduction-to-Statsmodels.ipynb ├── 2-EWMA-Exponentially-weighted-moving-average.ipynb ├── 3-ETS-Decomposition.ipynb ├── 4-ARIMA-and-Seasonal-ARIMA.ipynb ├── airline_passengers.csv └── monthly-milk-production-pounds-p.csv ├── 09-Python-Finance-Fundamentals ├── 01-Portfolio-Allocation-and-Sharpe-Ratio.ipynb ├── 02-Portfolio-Optimization.ipynb ├── 03-CAPM-Capital-Asset-Pricing-Model.ipynb ├── AAPL_CLOSE ├── AMZN_CLOSE ├── CISCO_CLOSE ├── GOOG_CLOSE ├── IBM_CLOSE └── walmart_stock.csv ├── 10-Quantopian-Platform ├── 01-Quantopian-Research-Basics.ipynb ├── 02-Basic-Algorithm-Methods.ipynb ├── 03-First-Trading-Algorithm.ipynb ├── 04-Trading-Algorithm-Exercise.ipynb ├── 05-Trading-Algorithm-Exercise-Solutions.ipynb └── 06-Pipelines.ipynb ├── 11-Advanced-Quantopian-Topics ├── 00-Pipeline-Example-Walkthrough.ipynb ├── 01-Leverage.ipynb ├── 02-Hedging.ipynb ├── 03-Portfolio-Analysis-with-Pyfolio.ipynb ├── 04-Stock-Sentiment-Analysis-Project.ipynb └── 05-Futures.ipynb └── README.md /01-Python-Crash-Course/Python Crash Course Exercises - Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Crash Course Exercises - Solutions\n", 8 | "\n", 9 | "This is an optional exercise to test your understanding of Python Basics. The questions tend to have a financial theme to them, but don't look to deeply into these tasks themselves, many of them don't hold any significance and are meaningless. If you find this extremely challenging, then you probably are not ready for the rest of this course yet and don't have enough programming experience to continue. I would suggest you take another course more geared towards complete beginners, such as [Complete Python Bootcamp]()" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Exercises\n", 17 | "\n", 18 | "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "### Task #1\n", 26 | "\n", 27 | "Given price = 300 , use python to figure out the square root of the price." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 3, 33 | "metadata": { 34 | "collapsed": true 35 | }, 36 | "outputs": [], 37 | "source": [ 38 | "price = 300" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 4, 44 | "metadata": { 45 | "collapsed": false 46 | }, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "17.320508075688775" 52 | ] 53 | }, 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "price**0.5" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 6, 66 | "metadata": { 67 | "collapsed": false 68 | }, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "17.320508075688775" 74 | ] 75 | }, 76 | "execution_count": 6, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "import math\n", 83 | "math.sqrt(price)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "### Task #2\n", 91 | "\n", 92 | "Given the string:\n", 93 | "\n", 94 | " stock_index = \"SP500\"\n", 95 | " \n", 96 | "Grab '500' from the string using indexing." 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 1, 102 | "metadata": { 103 | "collapsed": true 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "stock_index = \"SP500\"" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 2, 113 | "metadata": { 114 | "collapsed": false 115 | }, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "'500'" 121 | ] 122 | }, 123 | "execution_count": 2, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "stock_index[2:]" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "### Task #3\n", 137 | "\n", 138 | "** Given the variables:**\n", 139 | "\n", 140 | " stock_index = \"SP500\"\n", 141 | " price = 300\n", 142 | "\n", 143 | "** Use .format() to print the following string: **\n", 144 | "\n", 145 | " The SP500 is at 300 today." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 7, 151 | "metadata": { 152 | "collapsed": true 153 | }, 154 | "outputs": [], 155 | "source": [ 156 | "stock_index = \"SP500\"\n", 157 | "price = 300" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 9, 163 | "metadata": { 164 | "collapsed": false 165 | }, 166 | "outputs": [ 167 | { 168 | "name": "stdout", 169 | "output_type": "stream", 170 | "text": [ 171 | "The SP500 is at 300 today.\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "print(\"The {} is at {} today.\".format(stock_index,price))" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "### Task #4\n", 184 | "\n", 185 | "** Given the variable of a nested dictionary with nested lists: **\n", 186 | "\n", 187 | " stock_info = {'sp500':{'today':300,'yesterday': 250}, 'info':['Time',[24,7,365]]}\n", 188 | " \n", 189 | "** Use indexing and key calls to grab the following items:**\n", 190 | "\n", 191 | "* Yesterday's SP500 price (250)\n", 192 | "* The number 365 nested inside a list nested inside the 'info' key." 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 10, 198 | "metadata": { 199 | "collapsed": true 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "stock_info = {'sp500':{'today':300,'yesterday': 250}, 'info':['Time',[24,7,365]]}" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 12, 209 | "metadata": { 210 | "collapsed": false 211 | }, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/plain": [ 216 | "250" 217 | ] 218 | }, 219 | "execution_count": 12, 220 | "metadata": {}, 221 | "output_type": "execute_result" 222 | } 223 | ], 224 | "source": [ 225 | "stock_info['sp500']['yesterday']" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 15, 231 | "metadata": { 232 | "collapsed": false 233 | }, 234 | "outputs": [ 235 | { 236 | "data": { 237 | "text/plain": [ 238 | "365" 239 | ] 240 | }, 241 | "execution_count": 15, 242 | "metadata": {}, 243 | "output_type": "execute_result" 244 | } 245 | ], 246 | "source": [ 247 | "stock_info['info'][1][2]" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "### Task #5\n", 255 | "\n", 256 | "** Given strings with this form where the last source value is always separated by two dashes -- **\n", 257 | "\n", 258 | " \"PRICE:345.324:SOURCE--QUANDL\"\n", 259 | " \n", 260 | "**Create a function called source_finder() that returns the source. For example, the above string passed into the function would return \"QUANDL\"**" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 16, 266 | "metadata": { 267 | "collapsed": true 268 | }, 269 | "outputs": [], 270 | "source": [ 271 | "def source_finder(s):\n", 272 | " return s.split('--')[-1]" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 18, 278 | "metadata": { 279 | "collapsed": false 280 | }, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "'QUANDL'" 286 | ] 287 | }, 288 | "execution_count": 18, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "source_finder(\"PRICE:345.324:SOURCE--QUANDL\")" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "### Task #5\n", 302 | "\n", 303 | "** Create a function called price_finder that returns True if the word 'price' is in a string. Your function should work even if 'Price' is capitalized or next to punctuation ('price!') **" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 19, 309 | "metadata": { 310 | "collapsed": true 311 | }, 312 | "outputs": [], 313 | "source": [ 314 | "def price_finder(s):\n", 315 | " return 'price' in s.lower()" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 20, 321 | "metadata": { 322 | "collapsed": false 323 | }, 324 | "outputs": [ 325 | { 326 | "data": { 327 | "text/plain": [ 328 | "True" 329 | ] 330 | }, 331 | "execution_count": 20, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "price_finder(\"What is the price?\")" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 22, 343 | "metadata": { 344 | "collapsed": false 345 | }, 346 | "outputs": [ 347 | { 348 | "data": { 349 | "text/plain": [ 350 | "True" 351 | ] 352 | }, 353 | "execution_count": 22, 354 | "metadata": {}, 355 | "output_type": "execute_result" 356 | } 357 | ], 358 | "source": [ 359 | "price_finder(\"DUDE, WHAT IS PRICE!!!\")" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 23, 365 | "metadata": { 366 | "collapsed": false 367 | }, 368 | "outputs": [ 369 | { 370 | "data": { 371 | "text/plain": [ 372 | "True" 373 | ] 374 | }, 375 | "execution_count": 23, 376 | "metadata": {}, 377 | "output_type": "execute_result" 378 | } 379 | ], 380 | "source": [ 381 | "price_finder(\"The price is 300\")" 382 | ] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "### Task #6\n", 389 | "\n", 390 | "** Create a function called count_price() that counts the number of times the word \"price\" occurs in a string. Account for capitalization and if the word price is next to punctuation. **" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 46, 396 | "metadata": { 397 | "collapsed": false 398 | }, 399 | "outputs": [], 400 | "source": [ 401 | "def count_price(s):\n", 402 | " count = 0\n", 403 | " for word in s.lower().split():\n", 404 | " # Need to use in, can't use == or will get error with punctuation\n", 405 | " if 'price' in word:\n", 406 | " count += 1\n", 407 | " \n", 408 | " # Note the indentation!\n", 409 | " return count" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 43, 415 | "metadata": { 416 | "collapsed": true 417 | }, 418 | "outputs": [], 419 | "source": [ 420 | "# Simpler Alternative\n", 421 | "def count_price(s):\n", 422 | " return s.lower().count('price')" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 44, 428 | "metadata": { 429 | "collapsed": false 430 | }, 431 | "outputs": [], 432 | "source": [ 433 | "s = 'Wow that is a nice price, very nice Price! I said price 3 times.'" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 47, 439 | "metadata": { 440 | "collapsed": false 441 | }, 442 | "outputs": [ 443 | { 444 | "data": { 445 | "text/plain": [ 446 | "3" 447 | ] 448 | }, 449 | "execution_count": 47, 450 | "metadata": {}, 451 | "output_type": "execute_result" 452 | } 453 | ], 454 | "source": [ 455 | "count_price(s)" 456 | ] 457 | }, 458 | { 459 | "cell_type": "markdown", 460 | "metadata": {}, 461 | "source": [ 462 | "### Task #7\n", 463 | "\n", 464 | "**Create a function called avg_price that takes in a list of stock price numbers and calculates the average (Sum of the numbers divided by the number of elements in the list). It should return a float. **" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 27, 470 | "metadata": { 471 | "collapsed": true 472 | }, 473 | "outputs": [], 474 | "source": [ 475 | "def avg_price(stocks):\n", 476 | " return sum(stocks)/len(stocks) # Python 2 users should multiply numerator by 1.0" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 30, 482 | "metadata": { 483 | "collapsed": false 484 | }, 485 | "outputs": [ 486 | { 487 | "data": { 488 | "text/plain": [ 489 | "4.0" 490 | ] 491 | }, 492 | "execution_count": 30, 493 | "metadata": {}, 494 | "output_type": "execute_result" 495 | } 496 | ], 497 | "source": [ 498 | "avg_price([3,4,5])" 499 | ] 500 | }, 501 | { 502 | "cell_type": "markdown", 503 | "metadata": {}, 504 | "source": [ 505 | "# Great job!" 506 | ] 507 | } 508 | ], 509 | "metadata": { 510 | "anaconda-cloud": {}, 511 | "kernelspec": { 512 | "display_name": "Python [default]", 513 | "language": "python", 514 | "name": "python3" 515 | }, 516 | "language_info": { 517 | "codemirror_mode": { 518 | "name": "ipython", 519 | "version": 3 520 | }, 521 | "file_extension": ".py", 522 | "mimetype": "text/x-python", 523 | "name": "python", 524 | "nbconvert_exporter": "python", 525 | "pygments_lexer": "ipython3", 526 | "version": "3.5.3" 527 | } 528 | }, 529 | "nbformat": 4, 530 | "nbformat_minor": 0 531 | } 532 | -------------------------------------------------------------------------------- /01-Python-Crash-Course/Python Crash Course Exercises .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Crash Course Exercises \n", 8 | "\n", 9 | "This is an optional exercise to test your understanding of Python Basics. The questions tend to have a financial theme to them, but don't look to deeply into these tasks themselves, many of them don't hold any significance and are meaningless. If you find this extremely challenging, then you probably are not ready for the rest of this course yet and don't have enough programming experience to continue. I would suggest you take another course more geared towards complete beginners, such as [Complete Python Bootcamp](https://www.udemy.com/complete-python-bootcamp/)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Exercises\n", 17 | "\n", 18 | "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "### Task #1\n", 26 | "\n", 27 | "Given price = 300 , use python to figure out the square root of the price." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 3, 33 | "metadata": { 34 | "collapsed": true 35 | }, 36 | "outputs": [], 37 | "source": [ 38 | "price = 300" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 4, 44 | "metadata": { 45 | "collapsed": false 46 | }, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "17.320508075688775" 52 | ] 53 | }, 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 6, 64 | "metadata": { 65 | "collapsed": false 66 | }, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "17.320508075688775" 72 | ] 73 | }, 74 | "execution_count": 6, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "### Task #2\n", 86 | "\n", 87 | "Given the string:\n", 88 | "\n", 89 | " stock_index = \"SP500\"\n", 90 | " \n", 91 | "Grab '500' from the string using indexing." 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 1, 97 | "metadata": { 98 | "collapsed": true 99 | }, 100 | "outputs": [], 101 | "source": [ 102 | "stock_index = \"SP500\"" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 2, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "'500'" 116 | ] 117 | }, 118 | "execution_count": 2, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Task #3\n", 130 | "\n", 131 | "** Given the variables:**\n", 132 | "\n", 133 | " stock_index = \"SP500\"\n", 134 | " price = 300\n", 135 | "\n", 136 | "** Use .format() to print the following string: **\n", 137 | "\n", 138 | " The SP500 is at 300 today." 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 7, 144 | "metadata": { 145 | "collapsed": true 146 | }, 147 | "outputs": [], 148 | "source": [ 149 | "stock_index = \"SP500\"\n", 150 | "price = 300" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 9, 156 | "metadata": { 157 | "collapsed": false 158 | }, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "The SP500 is at 300 today.\n" 165 | ] 166 | } 167 | ], 168 | "source": [] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "### Task #4\n", 175 | "\n", 176 | "** Given the variable of a nested dictionary with nested lists: **\n", 177 | "\n", 178 | " stock_info = {'sp500':{'today':300,'yesterday': 250}, 'info':['Time',[24,7,365]]}\n", 179 | " \n", 180 | "** Use indexing and key calls to grab the following items:**\n", 181 | "\n", 182 | "* Yesterday's SP500 price (250)\n", 183 | "* The number 365 nested inside a list nested inside the 'info' key." 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 10, 189 | "metadata": { 190 | "collapsed": true 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "stock_info = {'sp500':{'today':300,'yesterday': 250}, 'info':['Time',[24,7,365]]}" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 12, 200 | "metadata": { 201 | "collapsed": false 202 | }, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "250" 208 | ] 209 | }, 210 | "execution_count": 12, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 15, 220 | "metadata": { 221 | "collapsed": false 222 | }, 223 | "outputs": [ 224 | { 225 | "data": { 226 | "text/plain": [ 227 | "365" 228 | ] 229 | }, 230 | "execution_count": 15, 231 | "metadata": {}, 232 | "output_type": "execute_result" 233 | } 234 | ], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "### Task #5\n", 242 | "\n", 243 | "** Given strings with this form where the last source value is always separated by two dashes -- **\n", 244 | "\n", 245 | " \"PRICE:345.324:SOURCE--QUANDL\"\n", 246 | " \n", 247 | "**Create a function called source_finder() that returns the source. For example, the above string passed into the function would return \"QUANDL\"**" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 16, 253 | "metadata": { 254 | "collapsed": true 255 | }, 256 | "outputs": [], 257 | "source": [] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 18, 262 | "metadata": { 263 | "collapsed": false 264 | }, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "'QUANDL'" 270 | ] 271 | }, 272 | "execution_count": 18, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "source_finder(\"PRICE:345.324:SOURCE--QUANDL\")" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "### Task #5\n", 286 | "\n", 287 | "** Create a function called price_finder that returns True if the word 'price' is in a string. Your function should work even if 'Price' is capitalized or next to punctuation ('price!') **" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 19, 293 | "metadata": { 294 | "collapsed": true 295 | }, 296 | "outputs": [], 297 | "source": [] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 20, 302 | "metadata": { 303 | "collapsed": false 304 | }, 305 | "outputs": [ 306 | { 307 | "data": { 308 | "text/plain": [ 309 | "True" 310 | ] 311 | }, 312 | "execution_count": 20, 313 | "metadata": {}, 314 | "output_type": "execute_result" 315 | } 316 | ], 317 | "source": [ 318 | "price_finder(\"What is the price?\")" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 22, 324 | "metadata": { 325 | "collapsed": false 326 | }, 327 | "outputs": [ 328 | { 329 | "data": { 330 | "text/plain": [ 331 | "True" 332 | ] 333 | }, 334 | "execution_count": 22, 335 | "metadata": {}, 336 | "output_type": "execute_result" 337 | } 338 | ], 339 | "source": [ 340 | "price_finder(\"DUDE, WHAT IS PRICE!!!\")" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 23, 346 | "metadata": { 347 | "collapsed": false 348 | }, 349 | "outputs": [ 350 | { 351 | "data": { 352 | "text/plain": [ 353 | "True" 354 | ] 355 | }, 356 | "execution_count": 23, 357 | "metadata": {}, 358 | "output_type": "execute_result" 359 | } 360 | ], 361 | "source": [ 362 | "price_finder(\"The price is 300\")" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "### Task #6\n", 370 | "\n", 371 | "** Create a function called count_price() that counts the number of times the word \"price\" occurs in a string. Account for capitalization and if the word price is next to punctuation. **" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 46, 377 | "metadata": { 378 | "collapsed": false 379 | }, 380 | "outputs": [], 381 | "source": [] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 44, 386 | "metadata": { 387 | "collapsed": false 388 | }, 389 | "outputs": [], 390 | "source": [ 391 | "s = 'Wow that is a nice price, very nice Price! I said price 3 times.'" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 47, 397 | "metadata": { 398 | "collapsed": false 399 | }, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "3" 405 | ] 406 | }, 407 | "execution_count": 47, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "count_price(s)" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "### Task #7\n", 421 | "\n", 422 | "**Create a function called avg_price that takes in a list of stock price numbers and calculates the average (Sum of the numbers divided by the number of elements in the list). It should return a float. **" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 27, 428 | "metadata": { 429 | "collapsed": true 430 | }, 431 | "outputs": [], 432 | "source": [] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 30, 437 | "metadata": { 438 | "collapsed": false 439 | }, 440 | "outputs": [ 441 | { 442 | "data": { 443 | "text/plain": [ 444 | "4.0" 445 | ] 446 | }, 447 | "execution_count": 30, 448 | "metadata": {}, 449 | "output_type": "execute_result" 450 | } 451 | ], 452 | "source": [ 453 | "avg_price([3,4,5])" 454 | ] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "# Great job!" 461 | ] 462 | } 463 | ], 464 | "metadata": { 465 | "anaconda-cloud": {}, 466 | "kernelspec": { 467 | "display_name": "Python [default]", 468 | "language": "python", 469 | "name": "python3" 470 | }, 471 | "language_info": { 472 | "codemirror_mode": { 473 | "name": "ipython", 474 | "version": 3 475 | }, 476 | "file_extension": ".py", 477 | "mimetype": "text/x-python", 478 | "name": "python", 479 | "nbconvert_exporter": "python", 480 | "pygments_lexer": "ipython3", 481 | "version": "3.5.3" 482 | } 483 | }, 484 | "nbformat": 4, 485 | "nbformat_minor": 0 486 | } 487 | -------------------------------------------------------------------------------- /02-NumPy/2-M-Numpy-Indexing-and-Selection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "___\n", 8 | "\n", 9 | " \n", 10 | "___\n", 11 | "
*Copyright Pierian Data 2017*
\n", 12 | "
*For more information, visit us at www.pieriandata.com*
" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "# NumPy Indexing and Selection\n", 20 | "\n", 21 | "In this lecture we will discuss how to select elements or groups of elements from an array." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import numpy as np" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "#Creating sample array\n", 40 | "arr = np.arange(0,11)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" 52 | ] 53 | }, 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "#Show\n", 61 | "arr" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "## Bracket Indexing and Selection\n", 69 | "The simplest way to pick one or some elements of an array looks very similar to python lists:" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "8" 81 | ] 82 | }, 83 | "execution_count": 4, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "#Get a value at an index\n", 90 | "arr[8]" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "array([1, 2, 3, 4])" 102 | ] 103 | }, 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "#Get values in a range\n", 111 | "arr[1:5]" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 6, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "array([0, 1, 2, 3, 4])" 123 | ] 124 | }, 125 | "execution_count": 6, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "#Get values in a range\n", 132 | "arr[0:5]" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "## Broadcasting\n", 140 | "\n", 141 | "Numpy arrays differ from a normal Python list because of their ability to broadcast:" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 7, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])" 153 | ] 154 | }, 155 | "execution_count": 7, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "#Setting a value with index range (Broadcasting)\n", 162 | "arr[0:5]=100\n", 163 | "\n", 164 | "#Show\n", 165 | "arr" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 8, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/plain": [ 176 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" 177 | ] 178 | }, 179 | "execution_count": 8, 180 | "metadata": {}, 181 | "output_type": "execute_result" 182 | } 183 | ], 184 | "source": [ 185 | "# Reset array, we'll see why I had to reset in a moment\n", 186 | "arr = np.arange(0,11)\n", 187 | "\n", 188 | "#Show\n", 189 | "arr" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "# Mark========================= " 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 9, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "array([0, 1, 2, 3, 4, 5])" 208 | ] 209 | }, 210 | "execution_count": 9, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [ 216 | "#Important notes on Slices\n", 217 | "slice_of_arr = arr[0:6]\n", 218 | "\n", 219 | "#Show slice\n", 220 | "slice_of_arr" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 10, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "array([99, 99, 99, 99, 99, 99])" 232 | ] 233 | }, 234 | "execution_count": 10, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "#Change Slice\n", 241 | "slice_of_arr[:]=99\n", 242 | "\n", 243 | "#Show Slice again\n", 244 | "slice_of_arr" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "# ============================== " 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "# Mark========================= " 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": {}, 264 | "source": [ 265 | "Now note the changes also occur in our original array!" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 11, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "data": { 275 | "text/plain": [ 276 | "array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])" 277 | ] 278 | }, 279 | "execution_count": 11, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "arr" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "Data is not copied, it's a view of the original array! This avoids memory problems!" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 12, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "data": { 302 | "text/plain": [ 303 | "array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])" 304 | ] 305 | }, 306 | "execution_count": 12, 307 | "metadata": {}, 308 | "output_type": "execute_result" 309 | } 310 | ], 311 | "source": [ 312 | "#To get a copy, need to be explicit\n", 313 | "arr_copy = arr.copy()\n", 314 | "\n", 315 | "arr_copy" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "# ============================= " 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "metadata": {}, 328 | "source": [ 329 | "## Indexing a 2D array (matrices)\n", 330 | "\n", 331 | "The general format is **arr_2d[row][col]** or **arr_2d[row,col]**. I recommend usually using the comma notation for clarity." 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 13, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "data": { 341 | "text/plain": [ 342 | "array([[ 5, 10, 15],\n", 343 | " [20, 25, 30],\n", 344 | " [35, 40, 45]])" 345 | ] 346 | }, 347 | "execution_count": 13, 348 | "metadata": {}, 349 | "output_type": "execute_result" 350 | } 351 | ], 352 | "source": [ 353 | "arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))\n", 354 | "\n", 355 | "#Show\n", 356 | "arr_2d" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 14, 362 | "metadata": {}, 363 | "outputs": [ 364 | { 365 | "data": { 366 | "text/plain": [ 367 | "array([20, 25, 30])" 368 | ] 369 | }, 370 | "execution_count": 14, 371 | "metadata": {}, 372 | "output_type": "execute_result" 373 | } 374 | ], 375 | "source": [ 376 | "#Indexing row\n", 377 | "arr_2d[1]\n" 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "metadata": {}, 383 | "source": [ 384 | "# Mark========================= " 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 15, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "data": { 394 | "text/plain": [ 395 | "20" 396 | ] 397 | }, 398 | "execution_count": 15, 399 | "metadata": {}, 400 | "output_type": "execute_result" 401 | } 402 | ], 403 | "source": [ 404 | "# Format is arr_2d[row][col] or arr_2d[row,col]\n", 405 | "\n", 406 | "# Getting individual element value\n", 407 | "arr_2d[1][0]" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 16, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "data": { 417 | "text/plain": [ 418 | "20" 419 | ] 420 | }, 421 | "execution_count": 16, 422 | "metadata": {}, 423 | "output_type": "execute_result" 424 | } 425 | ], 426 | "source": [ 427 | "# Getting individual element value\n", 428 | "arr_2d[1,0]" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 17, 434 | "metadata": {}, 435 | "outputs": [ 436 | { 437 | "data": { 438 | "text/plain": [ 439 | "array([[10, 15],\n", 440 | " [25, 30]])" 441 | ] 442 | }, 443 | "execution_count": 17, 444 | "metadata": {}, 445 | "output_type": "execute_result" 446 | } 447 | ], 448 | "source": [ 449 | "# 2D array slicing\n", 450 | "\n", 451 | "#Shape (2,2) from top right corner\n", 452 | "arr_2d[:2,1:]" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": {}, 458 | "source": [ 459 | "# =============================== " 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 18, 465 | "metadata": {}, 466 | "outputs": [ 467 | { 468 | "data": { 469 | "text/plain": [ 470 | "array([35, 40, 45])" 471 | ] 472 | }, 473 | "execution_count": 18, 474 | "metadata": {}, 475 | "output_type": "execute_result" 476 | } 477 | ], 478 | "source": [ 479 | "#Shape bottom row\n", 480 | "arr_2d[2]" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 19, 486 | "metadata": {}, 487 | "outputs": [ 488 | { 489 | "data": { 490 | "text/plain": [ 491 | "array([35, 40, 45])" 492 | ] 493 | }, 494 | "execution_count": 19, 495 | "metadata": {}, 496 | "output_type": "execute_result" 497 | } 498 | ], 499 | "source": [ 500 | "#Shape bottom row\n", 501 | "arr_2d[2,:]" 502 | ] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": {}, 507 | "source": [ 508 | "## More Indexing Help\n", 509 | "Indexing a 2d matrix can be a bit confusing at first, especially when you start to add in step size. Try google image searching NumPy indexing to fins useful images, like this one:\n", 510 | "\n", 511 | "" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "# Mark========================= " 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "## Conditional Selection\n", 526 | "\n", 527 | "This is a very fundamental concept that will directly translate to pandas later on, make sure you understand this part!\n", 528 | "\n", 529 | "Let's briefly go over how to use brackets for selection based off of comparison operators." 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "execution_count": 20, 535 | "metadata": {}, 536 | "outputs": [ 537 | { 538 | "data": { 539 | "text/plain": [ 540 | "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" 541 | ] 542 | }, 543 | "execution_count": 20, 544 | "metadata": {}, 545 | "output_type": "execute_result" 546 | } 547 | ], 548 | "source": [ 549 | "arr = np.arange(1,11)\n", 550 | "arr" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 21, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "data": { 560 | "text/plain": [ 561 | "array([False, False, False, False, True, True, True, True, True,\n", 562 | " True])" 563 | ] 564 | }, 565 | "execution_count": 21, 566 | "metadata": {}, 567 | "output_type": "execute_result" 568 | } 569 | ], 570 | "source": [ 571 | "arr > 4" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 22, 577 | "metadata": {}, 578 | "outputs": [], 579 | "source": [ 580 | "bool_arr = arr>4" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 23, 586 | "metadata": {}, 587 | "outputs": [ 588 | { 589 | "data": { 590 | "text/plain": [ 591 | "array([False, False, False, False, True, True, True, True, True,\n", 592 | " True])" 593 | ] 594 | }, 595 | "execution_count": 23, 596 | "metadata": {}, 597 | "output_type": "execute_result" 598 | } 599 | ], 600 | "source": [ 601 | "bool_arr" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 24, 607 | "metadata": {}, 608 | "outputs": [ 609 | { 610 | "data": { 611 | "text/plain": [ 612 | "array([ 5, 6, 7, 8, 9, 10])" 613 | ] 614 | }, 615 | "execution_count": 24, 616 | "metadata": {}, 617 | "output_type": "execute_result" 618 | } 619 | ], 620 | "source": [ 621 | "arr[bool_arr]" 622 | ] 623 | }, 624 | { 625 | "cell_type": "code", 626 | "execution_count": 25, 627 | "metadata": {}, 628 | "outputs": [ 629 | { 630 | "data": { 631 | "text/plain": [ 632 | "array([ 3, 4, 5, 6, 7, 8, 9, 10])" 633 | ] 634 | }, 635 | "execution_count": 25, 636 | "metadata": {}, 637 | "output_type": "execute_result" 638 | } 639 | ], 640 | "source": [ 641 | "arr[arr>2]" 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "execution_count": 26, 647 | "metadata": {}, 648 | "outputs": [ 649 | { 650 | "data": { 651 | "text/plain": [ 652 | "array([ 3, 4, 5, 6, 7, 8, 9, 10])" 653 | ] 654 | }, 655 | "execution_count": 26, 656 | "metadata": {}, 657 | "output_type": "execute_result" 658 | } 659 | ], 660 | "source": [ 661 | "x = 2\n", 662 | "arr[arr>x]" 663 | ] 664 | }, 665 | { 666 | "cell_type": "markdown", 667 | "metadata": {}, 668 | "source": [ 669 | "# ============================== " 670 | ] 671 | }, 672 | { 673 | "cell_type": "markdown", 674 | "metadata": {}, 675 | "source": [ 676 | "# Great Job!\n" 677 | ] 678 | } 679 | ], 680 | "metadata": { 681 | "anaconda-cloud": {}, 682 | "kernelspec": { 683 | "display_name": "Python 2", 684 | "language": "python", 685 | "name": "python2" 686 | }, 687 | "language_info": { 688 | "codemirror_mode": { 689 | "name": "ipython", 690 | "version": 2 691 | }, 692 | "file_extension": ".py", 693 | "mimetype": "text/x-python", 694 | "name": "python", 695 | "nbconvert_exporter": "python", 696 | "pygments_lexer": "ipython2", 697 | "version": "2.7.14" 698 | } 699 | }, 700 | "nbformat": 4, 701 | "nbformat_minor": 1 702 | } 703 | -------------------------------------------------------------------------------- /02-NumPy/3-M-Numpy-Operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "___\n", 8 | "\n", 9 | " \n", 10 | "___\n", 11 | "
*Copyright Pierian Data 2017*
\n", 12 | "
*For more information, visit us at www.pieriandata.com*
" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "source": [ 21 | "# NumPy Operations" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Arithmetic\n", 29 | "\n", 30 | "You can easily perform array with array arithmetic, or scalar with array arithmetic. Let's see some examples:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": { 37 | "collapsed": true 38 | }, 39 | "outputs": [], 40 | "source": [ 41 | "import numpy as np\n", 42 | "arr = np.arange(0,10)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])" 54 | ] 55 | }, 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "arr + arr" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])" 74 | ] 75 | }, 76 | "execution_count": 3, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "arr * arr" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 4, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" 94 | ] 95 | }, 96 | "execution_count": 4, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "arr - arr" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 5, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stderr", 112 | "output_type": "stream", 113 | "text": [ 114 | "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in true_divide\n", 115 | " if __name__ == '__main__':\n" 116 | ] 117 | }, 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "array([ nan, 1., 1., 1., 1., 1., 1., 1., 1., 1.])" 122 | ] 123 | }, 124 | "execution_count": 5, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "# Warning on division by zero, but not an error!\n", 131 | "# Just replaced with nan\n", 132 | "arr/arr" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 6, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stderr", 142 | "output_type": "stream", 143 | "text": [ 144 | "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in true_divide\n", 145 | " if __name__ == '__main__':\n" 146 | ] 147 | }, 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "array([ inf, 1. , 0.5 , 0.33333333, 0.25 ,\n", 152 | " 0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111])" 153 | ] 154 | }, 155 | "execution_count": 6, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "# Also warning, but not an error instead infinity\n", 162 | "1/arr" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 10, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "data": { 172 | "text/plain": [ 173 | "array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])" 174 | ] 175 | }, 176 | "execution_count": 10, 177 | "metadata": {}, 178 | "output_type": "execute_result" 179 | } 180 | ], 181 | "source": [ 182 | "arr**3" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "# Mark========================= " 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "## Universal Array Functions\n", 197 | "\n", 198 | "Numpy comes with many [universal array functions](http://docs.scipy.org/doc/numpy/reference/ufuncs.html), which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones:" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 12, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "array([ 0. , 1. , 1.41421356, 1.73205081, 2. ,\n", 210 | " 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])" 211 | ] 212 | }, 213 | "execution_count": 12, 214 | "metadata": {}, 215 | "output_type": "execute_result" 216 | } 217 | ], 218 | "source": [ 219 | "#Taking Square Roots\n", 220 | "np.sqrt(arr)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 13, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "array([ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00,\n", 232 | " 2.00855369e+01, 5.45981500e+01, 1.48413159e+02,\n", 233 | " 4.03428793e+02, 1.09663316e+03, 2.98095799e+03,\n", 234 | " 8.10308393e+03])" 235 | ] 236 | }, 237 | "execution_count": 13, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "#Calcualting exponential (e^)\n", 244 | "np.exp(arr)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 14, 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "data": { 254 | "text/plain": [ 255 | "9" 256 | ] 257 | }, 258 | "execution_count": 14, 259 | "metadata": {}, 260 | "output_type": "execute_result" 261 | } 262 | ], 263 | "source": [ 264 | "np.max(arr) #same as arr.max()" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 15, 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "data": { 274 | "text/plain": [ 275 | "array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,\n", 276 | " -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])" 277 | ] 278 | }, 279 | "execution_count": 15, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "np.sin(arr)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 16, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stderr", 295 | "output_type": "stream", 296 | "text": [ 297 | "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in log\n", 298 | " if __name__ == '__main__':\n" 299 | ] 300 | }, 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "array([ -inf, 0. , 0.69314718, 1.09861229, 1.38629436,\n", 305 | " 1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458])" 306 | ] 307 | }, 308 | "execution_count": 16, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "np.log(arr)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": {}, 320 | "source": [ 321 | "# ============================= " 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "# Great Job!\n", 329 | "\n", 330 | "That's all we need to know for now!" 331 | ] 332 | } 333 | ], 334 | "metadata": { 335 | "kernelspec": { 336 | "display_name": "Python 2", 337 | "language": "python", 338 | "name": "python2" 339 | }, 340 | "language_info": { 341 | "codemirror_mode": { 342 | "name": "ipython", 343 | "version": 2 344 | }, 345 | "file_extension": ".py", 346 | "mimetype": "text/x-python", 347 | "name": "python", 348 | "nbconvert_exporter": "python", 349 | "pygments_lexer": "ipython2", 350 | "version": "2.7.14" 351 | } 352 | }, 353 | "nbformat": 4, 354 | "nbformat_minor": 1 355 | } 356 | -------------------------------------------------------------------------------- /02-NumPy/d-Numpy Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "___\n", 8 | "\n", 9 | " \n", 10 | "___\n", 11 | "
*Copyright Pierian Data 2017*
\n", 12 | "
*For more information, visit us at www.pieriandata.com*
" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "# NumPy Exercises\n", 20 | "Now that we've learned about NumPy let's test your knowledge. We'll start off with a few simple tasks and then you'll be asked some more complicated questions.\n", 21 | "\n", 22 | "** IMPORTANT NOTE! Make sure you don't run the cells directly above the example output shown, otherwise you will end up writing over the example output! **" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "#### Import NumPy as np" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 1, 35 | "metadata": { 36 | "collapsed": true 37 | }, 38 | "outputs": [], 39 | "source": [] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "#### Create an array of 10 zeros " 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "# CODE HERE" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 2, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" 68 | ] 69 | }, 70 | "execution_count": 2, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "#### Create an array of 10 ones" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": { 88 | "collapsed": true 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "# CODE HERE" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 3, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" 104 | ] 105 | }, 106 | "execution_count": 3, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "#### Create an array of 10 fives" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": { 124 | "collapsed": true 125 | }, 126 | "outputs": [], 127 | "source": [ 128 | "# CODE HERE" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 4, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "array([ 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])" 140 | ] 141 | }, 142 | "execution_count": 4, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "#### Create an array of the integers from 10 to 50" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": { 160 | "collapsed": true 161 | }, 162 | "outputs": [], 163 | "source": [ 164 | "# CODE HERE" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 5, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "data": { 174 | "text/plain": [ 175 | "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n", 176 | " 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n", 177 | " 44, 45, 46, 47, 48, 49, 50])" 178 | ] 179 | }, 180 | "execution_count": 5, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "#### Create an array of all the even integers from 10 to 50" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "metadata": { 198 | "collapsed": true 199 | }, 200 | "outputs": [], 201 | "source": [ 202 | "# CODE HERE" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 6, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,\n", 214 | " 44, 46, 48, 50])" 215 | ] 216 | }, 217 | "execution_count": 6, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "#### Create a 3x3 matrix with values ranging from 0 to 8" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": { 235 | "collapsed": true 236 | }, 237 | "outputs": [], 238 | "source": [ 239 | "# CODE HERE" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 7, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "array([[0, 1, 2],\n", 251 | " [3, 4, 5],\n", 252 | " [6, 7, 8]])" 253 | ] 254 | }, 255 | "execution_count": 7, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "source": [] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "#### Create a 3x3 identity matrix" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": { 273 | "collapsed": true 274 | }, 275 | "outputs": [], 276 | "source": [ 277 | "# CODE HERE" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 8, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "array([[ 1., 0., 0.],\n", 289 | " [ 0., 1., 0.],\n", 290 | " [ 0., 0., 1.]])" 291 | ] 292 | }, 293 | "execution_count": 8, 294 | "metadata": {}, 295 | "output_type": "execute_result" 296 | } 297 | ], 298 | "source": [] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "#### Use NumPy to generate a random number between 0 and 1" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": { 311 | "collapsed": true 312 | }, 313 | "outputs": [], 314 | "source": [ 315 | "# CODE HERE" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 15, 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": [ 326 | "array([ 0.42829726])" 327 | ] 328 | }, 329 | "execution_count": 15, 330 | "metadata": {}, 331 | "output_type": "execute_result" 332 | } 333 | ], 334 | "source": [] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "#### Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": { 347 | "collapsed": true 348 | }, 349 | "outputs": [], 350 | "source": [ 351 | "# CODE HERE" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 33, 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "data": { 361 | "text/plain": [ 362 | "array([ 1.32031013, 1.6798602 , -0.42985892, -1.53116655, 0.85753232,\n", 363 | " 0.87339938, 0.35668636, -1.47491157, 0.15349697, 0.99530727,\n", 364 | " -0.94865451, -1.69174783, 1.57525349, -0.70615234, 0.10991879,\n", 365 | " -0.49478947, 1.08279872, 0.76488333, -2.3039931 , 0.35401124,\n", 366 | " -0.45454399, -0.64754649, -0.29391671, 0.02339861, 0.38272124])" 367 | ] 368 | }, 369 | "execution_count": 33, 370 | "metadata": {}, 371 | "output_type": "execute_result" 372 | } 373 | ], 374 | "source": [] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "metadata": {}, 379 | "source": [ 380 | "#### Create the following matrix:" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": { 387 | "collapsed": true 388 | }, 389 | "outputs": [], 390 | "source": [] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 35, 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "data": { 399 | "text/plain": [ 400 | "array([[ 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],\n", 401 | " [ 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],\n", 402 | " [ 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],\n", 403 | " [ 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],\n", 404 | " [ 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],\n", 405 | " [ 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],\n", 406 | " [ 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],\n", 407 | " [ 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],\n", 408 | " [ 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],\n", 409 | " [ 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])" 410 | ] 411 | }, 412 | "execution_count": 35, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "#### Create an array of 20 linearly spaced points between 0 and 1:" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": null, 429 | "metadata": { 430 | "collapsed": true 431 | }, 432 | "outputs": [], 433 | "source": [] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": 36, 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "data": { 442 | "text/plain": [ 443 | "array([ 0. , 0.05263158, 0.10526316, 0.15789474, 0.21052632,\n", 444 | " 0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,\n", 445 | " 0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,\n", 446 | " 0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ])" 447 | ] 448 | }, 449 | "execution_count": 36, 450 | "metadata": {}, 451 | "output_type": "execute_result" 452 | } 453 | ], 454 | "source": [] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "## Numpy Indexing and Selection\n", 461 | "\n", 462 | "Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 38, 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "data": { 472 | "text/plain": [ 473 | "array([[ 1, 2, 3, 4, 5],\n", 474 | " [ 6, 7, 8, 9, 10],\n", 475 | " [11, 12, 13, 14, 15],\n", 476 | " [16, 17, 18, 19, 20],\n", 477 | " [21, 22, 23, 24, 25]])" 478 | ] 479 | }, 480 | "execution_count": 38, 481 | "metadata": {}, 482 | "output_type": "execute_result" 483 | } 484 | ], 485 | "source": [ 486 | "# HERE IS THE GIVEN MATRIX CALLED MAT\n", 487 | "# USE IT FOR THE FOLLOWING TASKS\n", 488 | "mat = np.arange(1,26).reshape(5,5)\n", 489 | "mat" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 39, 495 | "metadata": { 496 | "collapsed": true 497 | }, 498 | "outputs": [], 499 | "source": [ 500 | "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n", 501 | "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n", 502 | "# BE ABLE TO SEE THE OUTPUT ANY MORE" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 40, 508 | "metadata": {}, 509 | "outputs": [ 510 | { 511 | "data": { 512 | "text/plain": [ 513 | "array([[12, 13, 14, 15],\n", 514 | " [17, 18, 19, 20],\n", 515 | " [22, 23, 24, 25]])" 516 | ] 517 | }, 518 | "execution_count": 40, 519 | "metadata": {}, 520 | "output_type": "execute_result" 521 | } 522 | ], 523 | "source": [] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 29, 528 | "metadata": { 529 | "collapsed": true 530 | }, 531 | "outputs": [], 532 | "source": [ 533 | "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n", 534 | "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n", 535 | "# BE ABLE TO SEE THE OUTPUT ANY MORE" 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": 41, 541 | "metadata": {}, 542 | "outputs": [ 543 | { 544 | "data": { 545 | "text/plain": [ 546 | "20" 547 | ] 548 | }, 549 | "execution_count": 41, 550 | "metadata": {}, 551 | "output_type": "execute_result" 552 | } 553 | ], 554 | "source": [] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 30, 559 | "metadata": { 560 | "collapsed": true 561 | }, 562 | "outputs": [], 563 | "source": [ 564 | "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n", 565 | "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n", 566 | "# BE ABLE TO SEE THE OUTPUT ANY MORE" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 42, 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "data": { 576 | "text/plain": [ 577 | "array([[ 2],\n", 578 | " [ 7],\n", 579 | " [12]])" 580 | ] 581 | }, 582 | "execution_count": 42, 583 | "metadata": {}, 584 | "output_type": "execute_result" 585 | } 586 | ], 587 | "source": [] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 31, 592 | "metadata": { 593 | "collapsed": true 594 | }, 595 | "outputs": [], 596 | "source": [ 597 | "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n", 598 | "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n", 599 | "# BE ABLE TO SEE THE OUTPUT ANY MORE" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 46, 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": [ 610 | "array([21, 22, 23, 24, 25])" 611 | ] 612 | }, 613 | "execution_count": 46, 614 | "metadata": {}, 615 | "output_type": "execute_result" 616 | } 617 | ], 618 | "source": [] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": 32, 623 | "metadata": { 624 | "collapsed": true 625 | }, 626 | "outputs": [], 627 | "source": [ 628 | "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW\n", 629 | "# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T\n", 630 | "# BE ABLE TO SEE THE OUTPUT ANY MORE" 631 | ] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "execution_count": 49, 636 | "metadata": {}, 637 | "outputs": [ 638 | { 639 | "data": { 640 | "text/plain": [ 641 | "array([[16, 17, 18, 19, 20],\n", 642 | " [21, 22, 23, 24, 25]])" 643 | ] 644 | }, 645 | "execution_count": 49, 646 | "metadata": {}, 647 | "output_type": "execute_result" 648 | } 649 | ], 650 | "source": [] 651 | }, 652 | { 653 | "cell_type": "markdown", 654 | "metadata": {}, 655 | "source": [ 656 | "### Now do the following" 657 | ] 658 | }, 659 | { 660 | "cell_type": "markdown", 661 | "metadata": {}, 662 | "source": [ 663 | "#### Get the sum of all the values in mat" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": null, 669 | "metadata": { 670 | "collapsed": true 671 | }, 672 | "outputs": [], 673 | "source": [ 674 | "# CODE HERE" 675 | ] 676 | }, 677 | { 678 | "cell_type": "code", 679 | "execution_count": 50, 680 | "metadata": {}, 681 | "outputs": [ 682 | { 683 | "data": { 684 | "text/plain": [ 685 | "325" 686 | ] 687 | }, 688 | "execution_count": 50, 689 | "metadata": {}, 690 | "output_type": "execute_result" 691 | } 692 | ], 693 | "source": [] 694 | }, 695 | { 696 | "cell_type": "markdown", 697 | "metadata": {}, 698 | "source": [ 699 | "#### Get the standard deviation of the values in mat" 700 | ] 701 | }, 702 | { 703 | "cell_type": "code", 704 | "execution_count": null, 705 | "metadata": { 706 | "collapsed": true 707 | }, 708 | "outputs": [], 709 | "source": [ 710 | "# CODE HERE" 711 | ] 712 | }, 713 | { 714 | "cell_type": "code", 715 | "execution_count": 51, 716 | "metadata": {}, 717 | "outputs": [ 718 | { 719 | "data": { 720 | "text/plain": [ 721 | "7.2111025509279782" 722 | ] 723 | }, 724 | "execution_count": 51, 725 | "metadata": {}, 726 | "output_type": "execute_result" 727 | } 728 | ], 729 | "source": [] 730 | }, 731 | { 732 | "cell_type": "markdown", 733 | "metadata": {}, 734 | "source": [ 735 | "#### Get the sum of all the columns in mat" 736 | ] 737 | }, 738 | { 739 | "cell_type": "code", 740 | "execution_count": null, 741 | "metadata": { 742 | "collapsed": true 743 | }, 744 | "outputs": [], 745 | "source": [ 746 | "# CODE HERE" 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": 53, 752 | "metadata": {}, 753 | "outputs": [ 754 | { 755 | "data": { 756 | "text/plain": [ 757 | "array([55, 60, 65, 70, 75])" 758 | ] 759 | }, 760 | "execution_count": 53, 761 | "metadata": {}, 762 | "output_type": "execute_result" 763 | } 764 | ], 765 | "source": [] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": {}, 770 | "source": [ 771 | "## Bonus Question\n", 772 | "\n", 773 | "We worked a lot with random data with numpy, but is there a way we can insure that we always get the same random numbers? [Click Here for a Hint](https://www.google.com/search?q=numpy+random+seed&rlz=1C1CHBF_enUS747US747&oq=numpy+random+seed&aqs=chrome..69i57j69i60j0l4.2087j0j7&sourceid=chrome&ie=UTF-8)" 774 | ] 775 | }, 776 | { 777 | "cell_type": "code", 778 | "execution_count": null, 779 | "metadata": { 780 | "collapsed": true 781 | }, 782 | "outputs": [], 783 | "source": [] 784 | }, 785 | { 786 | "cell_type": "markdown", 787 | "metadata": { 788 | "collapsed": true 789 | }, 790 | "source": [ 791 | "# Great Job!" 792 | ] 793 | } 794 | ], 795 | "metadata": { 796 | "anaconda-cloud": {}, 797 | "kernelspec": { 798 | "display_name": "Python 3", 799 | "language": "python", 800 | "name": "python3" 801 | }, 802 | "language_info": { 803 | "codemirror_mode": { 804 | "name": "ipython", 805 | "version": 3 806 | }, 807 | "file_extension": ".py", 808 | "mimetype": "text/x-python", 809 | "name": "python", 810 | "nbconvert_exporter": "python", 811 | "pygments_lexer": "ipython3", 812 | "version": "3.6.1" 813 | } 814 | }, 815 | "nbformat": 4, 816 | "nbformat_minor": 1 817 | } 818 | -------------------------------------------------------------------------------- /03- General Pandas/01-d-Introduction-to-Pandas.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "___\n", 8 | "\n", 9 | " \n", 10 | "___" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "source": [ 19 | "# Introduction to Pandas\n", 20 | "\n", 21 | "In this section of the course we will learn how to use pandas for data analysis. You can think of pandas as an extremely powerful version of Excel, with a lot more features. In this section of the course, you should go through the notebooks in this order:\n", 22 | "\n", 23 | "* Introduction to Pandas\n", 24 | "* Series\n", 25 | "* DataFrames\n", 26 | "* Missing Data\n", 27 | "* GroupBy\n", 28 | "* Merging,Joining,and Concatenating\n", 29 | "* Operations\n", 30 | "* Data Input and Output" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "___" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.6.1" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 1 62 | } 63 | -------------------------------------------------------------------------------- /03- General Pandas/02-M-Series.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "___\n", 8 | "\n", 9 | " \n", 10 | "___\n", 11 | "# Series" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "The first main data type we will learn about for pandas is the Series data type. Let's import Pandas and explore the Series object.\n", 19 | "\n", 20 | "A Series is very similar to a NumPy array (in fact it is built on top of the NumPy array object). What differentiates the NumPy array from a Series, is that a Series can have axis labels, meaning it can be indexed by a label, instead of just a number location. It also doesn't need to hold numeric data, it can hold any arbitrary Python Object.\n", 21 | "\n", 22 | "Let's explore this concept through some examples:" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "collapsed": true 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "import numpy as np\n", 34 | "import pandas as pd" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "### Creating a Series\n", 42 | "\n", 43 | "You can convert a list,numpy array, or dictionary to a Series:" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "metadata": { 50 | "collapsed": true 51 | }, 52 | "outputs": [], 53 | "source": [ 54 | "labels = ['a','b','c']\n", 55 | "my_list = [10,20,30]\n", 56 | "arr = np.array([10,20,30])\n", 57 | "d = {'a':10,'b':20,'c':30}" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "# Mark========================= " 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "** Using Lists**" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 4, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "0 10\n", 83 | "1 20\n", 84 | "2 30\n", 85 | "dtype: int64" 86 | ] 87 | }, 88 | "execution_count": 4, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "pd.Series(data=my_list)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 5, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "a 10\n", 106 | "b 20\n", 107 | "c 30\n", 108 | "dtype: int64" 109 | ] 110 | }, 111 | "execution_count": 5, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "pd.Series(data=my_list,index=labels)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 6, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "a 10\n", 129 | "b 20\n", 130 | "c 30\n", 131 | "dtype: int64" 132 | ] 133 | }, 134 | "execution_count": 6, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "pd.Series(my_list,labels)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "** NumPy Arrays **" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 7, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "0 10\n", 159 | "1 20\n", 160 | "2 30\n", 161 | "dtype: int64" 162 | ] 163 | }, 164 | "execution_count": 7, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "pd.Series(arr)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 8, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "a 10\n", 182 | "b 20\n", 183 | "c 30\n", 184 | "dtype: int64" 185 | ] 186 | }, 187 | "execution_count": 8, 188 | "metadata": {}, 189 | "output_type": "execute_result" 190 | } 191 | ], 192 | "source": [ 193 | "pd.Series(arr,labels)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "** Dictionary**" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 9, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "a 10\n", 212 | "b 20\n", 213 | "c 30\n", 214 | "dtype: int64" 215 | ] 216 | }, 217 | "execution_count": 9, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "pd.Series(d)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "### Data in a Series\n", 231 | "\n", 232 | "A pandas Series can hold a variety of object types:" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 10, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "data": { 242 | "text/plain": [ 243 | "0 a\n", 244 | "1 b\n", 245 | "2 c\n", 246 | "dtype: object" 247 | ] 248 | }, 249 | "execution_count": 10, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "pd.Series(data=labels)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "# =============================== " 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 11, 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "data": { 272 | "text/plain": [ 273 | "0 \n", 274 | "1 \n", 275 | "2 \n", 276 | "dtype: object" 277 | ] 278 | }, 279 | "execution_count": 11, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "# Even functions (although unlikely that you will use this)\n", 286 | "pd.Series([sum,print,len])" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": {}, 292 | "source": [ 293 | "# Mark========================= " 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "metadata": {}, 299 | "source": [ 300 | "## Using an Index\n", 301 | "\n", 302 | "The key to using a Series is understanding its index. Pandas makes use of these index names or numbers by allowing for fast look ups of information (works like a hash table or dictionary).\n", 303 | "\n", 304 | "Let's see some examples of how to grab information from a Series. Let us create two sereis, ser1 and ser2:" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 12, 310 | "metadata": {}, 311 | "outputs": [], 312 | "source": [ 313 | "ser1 = pd.Series([1,2,3,4],index = ['USA', 'Germany','USSR', 'Japan']) " 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 13, 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "data": { 323 | "text/plain": [ 324 | "USA 1\n", 325 | "Germany 2\n", 326 | "USSR 3\n", 327 | "Japan 4\n", 328 | "dtype: int64" 329 | ] 330 | }, 331 | "execution_count": 13, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "ser1" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 14, 343 | "metadata": { 344 | "collapsed": true 345 | }, 346 | "outputs": [], 347 | "source": [ 348 | "ser2 = pd.Series([1,2,5,4],index = ['USA', 'Germany','Italy', 'Japan']) " 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 15, 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "data": { 358 | "text/plain": [ 359 | "USA 1\n", 360 | "Germany 2\n", 361 | "Italy 5\n", 362 | "Japan 4\n", 363 | "dtype: int64" 364 | ] 365 | }, 366 | "execution_count": 15, 367 | "metadata": {}, 368 | "output_type": "execute_result" 369 | } 370 | ], 371 | "source": [ 372 | "ser2" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 16, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/plain": [ 383 | "1" 384 | ] 385 | }, 386 | "execution_count": 16, 387 | "metadata": {}, 388 | "output_type": "execute_result" 389 | } 390 | ], 391 | "source": [ 392 | "ser1['USA']" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "Operations are then also done based off of index:" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 17, 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "data": { 409 | "text/plain": [ 410 | "Germany 4.0\n", 411 | "Italy NaN\n", 412 | "Japan 8.0\n", 413 | "USA 2.0\n", 414 | "USSR NaN\n", 415 | "dtype: float64" 416 | ] 417 | }, 418 | "execution_count": 17, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "ser1 + ser2" 425 | ] 426 | }, 427 | { 428 | "cell_type": "markdown", 429 | "metadata": {}, 430 | "source": [ 431 | "# =============================== " 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "metadata": {}, 437 | "source": [ 438 | "Let's stop here for now and move on to DataFrames, which will expand on the concept of Series!\n", 439 | "# Great Job!" 440 | ] 441 | } 442 | ], 443 | "metadata": { 444 | "kernelspec": { 445 | "display_name": "Python 2", 446 | "language": "python", 447 | "name": "python2" 448 | }, 449 | "language_info": { 450 | "codemirror_mode": { 451 | "name": "ipython", 452 | "version": 2 453 | }, 454 | "file_extension": ".py", 455 | "mimetype": "text/x-python", 456 | "name": "python", 457 | "nbconvert_exporter": "python", 458 | "pygments_lexer": "ipython2", 459 | "version": "2.7.14" 460 | } 461 | }, 462 | "nbformat": 4, 463 | "nbformat_minor": 1 464 | } 465 | -------------------------------------------------------------------------------- /03- General Pandas/04-M-Missing-Data.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "___\n", 8 | "\n", 9 | " \n", 10 | "___" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "# Mark========================= " 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "# Missing Data\n", 25 | "\n", 26 | "Let's show a few convenient methods to deal with Missing Data in pandas:" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "import numpy as np\n", 36 | "import pandas as pd" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "df = pd.DataFrame({'A':[1,2,np.nan],\n", 46 | " 'B':[5,np.nan,np.nan],\n", 47 | " 'C':[1,2,3]})" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/html": [ 58 | "
\n", 59 | "\n", 72 | "\n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | "
ABC
01.05.01
12.0NaN2
2NaNNaN3
\n", 102 | "
" 103 | ], 104 | "text/plain": [ 105 | " A B C\n", 106 | "0 1.0 5.0 1\n", 107 | "1 2.0 NaN 2\n", 108 | "2 NaN NaN 3" 109 | ] 110 | }, 111 | "execution_count": 3, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "df" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 4, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/html": [ 128 | "
\n", 129 | "\n", 142 | "\n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | "
ABC
01.05.01
\n", 160 | "
" 161 | ], 162 | "text/plain": [ 163 | " A B C\n", 164 | "0 1.0 5.0 1" 165 | ] 166 | }, 167 | "execution_count": 4, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "df.dropna() #if a row has Nan, then drop this row" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 5, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "data": { 183 | "text/html": [ 184 | "
\n", 185 | "\n", 198 | "\n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | "
C
01
12
23
\n", 220 | "
" 221 | ], 222 | "text/plain": [ 223 | " C\n", 224 | "0 1\n", 225 | "1 2\n", 226 | "2 3" 227 | ] 228 | }, 229 | "execution_count": 5, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "df.dropna(axis=1) #if a column has Nan, then drop this column" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 6, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/html": [ 246 | "
\n", 247 | "\n", 260 | "\n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | "
ABC
01.05.01
12.0NaN2
\n", 284 | "
" 285 | ], 286 | "text/plain": [ 287 | " A B C\n", 288 | "0 1.0 5.0 1\n", 289 | "1 2.0 NaN 2" 290 | ] 291 | }, 292 | "execution_count": 6, 293 | "metadata": {}, 294 | "output_type": "execute_result" 295 | } 296 | ], 297 | "source": [ 298 | "df.dropna(thresh=2) #if a row has at least 2 na, then drop this row" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 7, 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "data": { 308 | "text/html": [ 309 | "
\n", 310 | "\n", 323 | "\n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | "
AC
01.01
12.02
2NaN3
\n", 349 | "
" 350 | ], 351 | "text/plain": [ 352 | " A C\n", 353 | "0 1.0 1\n", 354 | "1 2.0 2\n", 355 | "2 NaN 3" 356 | ] 357 | }, 358 | "execution_count": 7, 359 | "metadata": {}, 360 | "output_type": "execute_result" 361 | } 362 | ], 363 | "source": [ 364 | "df.dropna(thresh=2,axis=1) #if a column has at least 2 na, then drop this column" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 8, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/html": [ 375 | "
\n", 376 | "\n", 389 | "\n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | "
ABC
0151
12FILL VALUE2
2FILL VALUEFILL VALUE3
\n", 419 | "
" 420 | ], 421 | "text/plain": [ 422 | " A B C\n", 423 | "0 1 5 1\n", 424 | "1 2 FILL VALUE 2\n", 425 | "2 FILL VALUE FILL VALUE 3" 426 | ] 427 | }, 428 | "execution_count": 8, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "df.fillna(value='FILL VALUE')" 435 | ] 436 | }, 437 | { 438 | "cell_type": "code", 439 | "execution_count": 9, 440 | "metadata": {}, 441 | "outputs": [ 442 | { 443 | "data": { 444 | "text/plain": [ 445 | "0 1.0\n", 446 | "1 2.0\n", 447 | "2 1.5\n", 448 | "Name: A, dtype: float64" 449 | ] 450 | }, 451 | "execution_count": 9, 452 | "metadata": {}, 453 | "output_type": "execute_result" 454 | } 455 | ], 456 | "source": [ 457 | "df['A'].fillna(value=df['A'].mean())" 458 | ] 459 | }, 460 | { 461 | "cell_type": "markdown", 462 | "metadata": {}, 463 | "source": [ 464 | "# =============================== " 465 | ] 466 | }, 467 | { 468 | "cell_type": "markdown", 469 | "metadata": {}, 470 | "source": [ 471 | "# Great Job!" 472 | ] 473 | } 474 | ], 475 | "metadata": { 476 | "kernelspec": { 477 | "display_name": "Python 2", 478 | "language": "python", 479 | "name": "python2" 480 | }, 481 | "language_info": { 482 | "codemirror_mode": { 483 | "name": "ipython", 484 | "version": 2 485 | }, 486 | "file_extension": ".py", 487 | "mimetype": "text/x-python", 488 | "name": "python", 489 | "nbconvert_exporter": "python", 490 | "pygments_lexer": "ipython2", 491 | "version": "2.7.14" 492 | } 493 | }, 494 | "nbformat": 4, 495 | "nbformat_minor": 1 496 | } 497 | -------------------------------------------------------------------------------- /03- General Pandas/Excel_Sample.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BessieChen/Python-for-Financial-Analysis-and-Algorithmic-Trading/849c322ccaffa10433f2f09e53f5907fc21fb0d7/03- General Pandas/Excel_Sample.xlsx -------------------------------------------------------------------------------- /03- General Pandas/Pandas-Exercises/Pandas Exercise SOLUTIONS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Pandas Exercise - Solutions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Time to test your new pandas skills! Use the two csv files in this folder to complete the tasks in bold below!\n", 15 | "\n", 16 | "** NOTE: ALL TASKS MUST BE DONE IN ONE LINE OF PANDAS CODE. GOT STUCK? NO PROBLEM! CHECK OUT THE SOLUTIONS LECTURE! **" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "** Import pandas and read in the banklist.csv file into a dataframe called banks. **" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "import pandas as pd" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 25, 40 | "metadata": { 41 | "collapsed": true 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "banks = pd.read_csv('banklist.csv')" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "** Show the head of the dataframe **" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 26, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [ 63 | "# CODE HERE" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 37, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/html": [ 74 | "
\n", 75 | "\n", 88 | "\n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | "
Bank NameCitySTCERTAcquiring InstitutionClosing DateUpdated Date
0Fayette County BankSaint ElmoIL1802United Fidelity Bank, fsb26-May-171-Jun-17
1Guaranty Bank, (d/b/a BestBank in Georgia & Mi...MilwaukeeWI30003First-Citizens Bank & Trust Company5-May-171-Jun-17
2First NBC BankNew OrleansLA58302Whitney Bank28-Apr-1723-May-17
3Proficio BankCottonwood HeightsUT35495Cache Valley Bank3-Mar-1718-May-17
4Seaway Bank and Trust CompanyChicagoIL19328State Bank of Texas27-Jan-1718-May-17
\n", 154 | "
" 155 | ], 156 | "text/plain": [ 157 | " Bank Name City ST \\\n", 158 | "0 Fayette County Bank Saint Elmo IL \n", 159 | "1 Guaranty Bank, (d/b/a BestBank in Georgia & Mi... Milwaukee WI \n", 160 | "2 First NBC Bank New Orleans LA \n", 161 | "3 Proficio Bank Cottonwood Heights UT \n", 162 | "4 Seaway Bank and Trust Company Chicago IL \n", 163 | "\n", 164 | " CERT Acquiring Institution Closing Date Updated Date \n", 165 | "0 1802 United Fidelity Bank, fsb 26-May-17 1-Jun-17 \n", 166 | "1 30003 First-Citizens Bank & Trust Company 5-May-17 1-Jun-17 \n", 167 | "2 58302 Whitney Bank 28-Apr-17 23-May-17 \n", 168 | "3 35495 Cache Valley Bank 3-Mar-17 18-May-17 \n", 169 | "4 19328 State Bank of Texas 27-Jan-17 18-May-17 " 170 | ] 171 | }, 172 | "execution_count": 37, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "banks.head()" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "** What are the column names? **" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "metadata": { 192 | "collapsed": true 193 | }, 194 | "outputs": [], 195 | "source": [ 196 | "# CODE HERE" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 29, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "Index(['Bank Name', 'City', 'ST', 'CERT', 'Acquiring Institution',\n", 208 | " 'Closing Date', 'Updated Date'],\n", 209 | " dtype='object')" 210 | ] 211 | }, 212 | "execution_count": 29, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "banks.columns" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "** How many States (ST) are represented in this data set? **" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": { 232 | "collapsed": true 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "# CODE HERE" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 33, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "44" 248 | ] 249 | }, 250 | "execution_count": 33, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "banks['ST'].nunique()" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "** Get a list or array of all the states in the data set. **" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": { 270 | "collapsed": true 271 | }, 272 | "outputs": [], 273 | "source": [ 274 | "# CODE HERE" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 32, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "array(['IL', 'WI', 'LA', 'UT', 'NJ', 'AR', 'GA', 'PA', 'TN', 'WA', 'CO',\n", 286 | " 'PR', 'FL', 'MN', 'CA', 'MD', 'OK', 'OH', 'SC', 'VA', 'ID', 'TX',\n", 287 | " 'CT', 'AZ', 'NV', 'NC', 'KY', 'MO', 'KS', 'AL', 'MI', 'IN', 'IA',\n", 288 | " 'NE', 'MS', 'NM', 'OR', 'NY', 'MA', 'SD', 'WY', 'WV', 'NH', 'HI'], dtype=object)" 289 | ] 290 | }, 291 | "execution_count": 32, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "banks['ST'].unique()" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "** What are the top 5 states with the most failed banks? **" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": { 311 | "collapsed": true, 312 | "scrolled": true 313 | }, 314 | "outputs": [], 315 | "source": [ 316 | "# CODE HERE" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 35, 322 | "metadata": {}, 323 | "outputs": [ 324 | { 325 | "data": { 326 | "text/plain": [ 327 | "ST\n", 328 | "GA 93\n", 329 | "FL 75\n", 330 | "IL 67\n", 331 | "CA 41\n", 332 | "MN 23\n", 333 | "Name: Bank Name, dtype: int64" 334 | ] 335 | }, 336 | "execution_count": 35, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "banks.groupby(\"ST\").count().sort_values('Bank Name',ascending=False).iloc[:5]['Bank Name']" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "** What are the top 5 acquiring institutions? **" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": { 356 | "collapsed": true 357 | }, 358 | "outputs": [], 359 | "source": [ 360 | "# CODE HERE" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 14, 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "data": { 370 | "text/plain": [ 371 | "No Acquirer 31\n", 372 | "State Bank and Trust Company 12\n", 373 | "First-Citizens Bank & Trust Company 11\n", 374 | "Ameris Bank 10\n", 375 | "U.S. Bank N.A. 9\n", 376 | "Name: Acquiring Institution, dtype: int64" 377 | ] 378 | }, 379 | "execution_count": 14, 380 | "metadata": {}, 381 | "output_type": "execute_result" 382 | } 383 | ], 384 | "source": [ 385 | "banks['Acquiring Institution'].value_counts().iloc[:5]" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "** How many banks has the State Bank of Texas acquired? How many of them were actually in Texas?**" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": null, 398 | "metadata": { 399 | "collapsed": true 400 | }, 401 | "outputs": [], 402 | "source": [ 403 | "# CODE HERE" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 15, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "data": { 413 | "text/html": [ 414 | "
\n", 415 | "\n", 428 | "\n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | "
Bank NameCitySTCERTAcquiring InstitutionClosing DateUpdated Date
4Seaway Bank and Trust CompanyChicagoIL19328State Bank of Texas27-Jan-1718-May-17
21The National Republic Bank of ChicagoChicagoIL916State Bank of Texas24-Oct-146-Jan-16
450Millennium State Bank of TexasDallasTX57667State Bank of Texas2-Jul-0926-Oct-12
\n", 474 | "
" 475 | ], 476 | "text/plain": [ 477 | " Bank Name City ST CERT \\\n", 478 | "4 Seaway Bank and Trust Company Chicago IL 19328 \n", 479 | "21 The National Republic Bank of Chicago Chicago IL 916 \n", 480 | "450 Millennium State Bank of Texas Dallas TX 57667 \n", 481 | "\n", 482 | " Acquiring Institution Closing Date Updated Date \n", 483 | "4 State Bank of Texas 27-Jan-17 18-May-17 \n", 484 | "21 State Bank of Texas 24-Oct-14 6-Jan-16 \n", 485 | "450 State Bank of Texas 2-Jul-09 26-Oct-12 " 486 | ] 487 | }, 488 | "execution_count": 15, 489 | "metadata": {}, 490 | "output_type": "execute_result" 491 | } 492 | ], 493 | "source": [ 494 | "banks[banks['Acquiring Institution']=='State Bank of Texas']" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "** What is the most common city in California for a bank to fail in?**" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": null, 507 | "metadata": { 508 | "collapsed": true 509 | }, 510 | "outputs": [], 511 | "source": [ 512 | "# CODE HERE" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 24, 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "data": { 522 | "text/html": [ 523 | "
\n", 524 | "\n", 537 | "\n", 538 | " \n", 539 | " \n", 540 | " \n", 541 | " \n", 542 | " \n", 543 | " \n", 544 | " \n", 545 | " \n", 546 | " \n", 547 | " \n", 548 | " \n", 549 | " \n", 550 | " \n", 551 | " \n", 552 | " \n", 553 | " \n", 554 | " \n", 555 | " \n", 556 | " \n", 557 | " \n", 558 | " \n", 559 | " \n", 560 | " \n", 561 | " \n", 562 | " \n", 563 | " \n", 564 | " \n", 565 | " \n", 566 | " \n", 567 | " \n", 568 | " \n", 569 | "
Bank NameSTCERTAcquiring InstitutionClosing DateUpdated Date
City
Los Angeles444444
\n", 570 | "
" 571 | ], 572 | "text/plain": [ 573 | " Bank Name ST CERT Acquiring Institution Closing Date \\\n", 574 | "City \n", 575 | "Los Angeles 4 4 4 4 4 \n", 576 | "\n", 577 | " Updated Date \n", 578 | "City \n", 579 | "Los Angeles 4 " 580 | ] 581 | }, 582 | "execution_count": 24, 583 | "metadata": {}, 584 | "output_type": "execute_result" 585 | } 586 | ], 587 | "source": [ 588 | "banks[banks['ST']=='CA'].groupby('City').count().sort_values('Bank Name',ascending=False).head(1)" 589 | ] 590 | }, 591 | { 592 | "cell_type": "markdown", 593 | "metadata": {}, 594 | "source": [ 595 | "** How many failed banks don't have the word \"Bank\" in their name? **" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": null, 601 | "metadata": { 602 | "collapsed": true 603 | }, 604 | "outputs": [], 605 | "source": [ 606 | "# CODE HERE" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": 55, 612 | "metadata": {}, 613 | "outputs": [ 614 | { 615 | "data": { 616 | "text/plain": [ 617 | "14" 618 | ] 619 | }, 620 | "execution_count": 55, 621 | "metadata": {}, 622 | "output_type": "execute_result" 623 | } 624 | ], 625 | "source": [ 626 | "# banks['Bank Name'].apply(lambda name: 'Bank' not in name).value_counts()\n", 627 | "sum(banks['Bank Name'].apply(lambda name: 'Bank' not in name))" 628 | ] 629 | }, 630 | { 631 | "cell_type": "markdown", 632 | "metadata": {}, 633 | "source": [ 634 | "** How many bank names start with the letter 's' ? **" 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "execution_count": null, 640 | "metadata": { 641 | "collapsed": true 642 | }, 643 | "outputs": [], 644 | "source": [ 645 | "# CODE HERE" 646 | ] 647 | }, 648 | { 649 | "cell_type": "code", 650 | "execution_count": 58, 651 | "metadata": {}, 652 | "outputs": [ 653 | { 654 | "data": { 655 | "text/plain": [ 656 | "53" 657 | ] 658 | }, 659 | "execution_count": 58, 660 | "metadata": {}, 661 | "output_type": "execute_result" 662 | } 663 | ], 664 | "source": [ 665 | "sum(banks['Bank Name'].apply(lambda name:name[0].upper() =='S'))" 666 | ] 667 | }, 668 | { 669 | "cell_type": "markdown", 670 | "metadata": {}, 671 | "source": [ 672 | "** How many CERT values are above 20000 ? **" 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": 61, 678 | "metadata": { 679 | "collapsed": true 680 | }, 681 | "outputs": [], 682 | "source": [ 683 | "# CODE HERE" 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": 64, 689 | "metadata": {}, 690 | "outputs": [ 691 | { 692 | "data": { 693 | "text/plain": [ 694 | "417" 695 | ] 696 | }, 697 | "execution_count": 64, 698 | "metadata": {}, 699 | "output_type": "execute_result" 700 | } 701 | ], 702 | "source": [ 703 | "sum(banks['CERT']>20000)" 704 | ] 705 | }, 706 | { 707 | "cell_type": "markdown", 708 | "metadata": {}, 709 | "source": [ 710 | "** How many bank names consist of just two words? (e.g. \"First Bank\" , \"Bank Georgia\" )**" 711 | ] 712 | }, 713 | { 714 | "cell_type": "code", 715 | "execution_count": 65, 716 | "metadata": { 717 | "collapsed": true 718 | }, 719 | "outputs": [], 720 | "source": [ 721 | "# CODE HERE" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 67, 727 | "metadata": {}, 728 | "outputs": [ 729 | { 730 | "data": { 731 | "text/plain": [ 732 | "114" 733 | ] 734 | }, 735 | "execution_count": 67, 736 | "metadata": {}, 737 | "output_type": "execute_result" 738 | } 739 | ], 740 | "source": [ 741 | "sum(banks['Bank Name'].apply(lambda name: len(name.split())==2))" 742 | ] 743 | }, 744 | { 745 | "cell_type": "markdown", 746 | "metadata": {}, 747 | "source": [ 748 | "**Bonus: How many banks closed in the year 2008? (this is hard because we technically haven't learned about time series with pandas yet! Feel free to skip this one!**" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": null, 754 | "metadata": { 755 | "collapsed": true 756 | }, 757 | "outputs": [], 758 | "source": [ 759 | "# CODE HERE" 760 | ] 761 | }, 762 | { 763 | "cell_type": "code", 764 | "execution_count": 54, 765 | "metadata": {}, 766 | "outputs": [ 767 | { 768 | "data": { 769 | "text/plain": [ 770 | "25" 771 | ] 772 | }, 773 | "execution_count": 54, 774 | "metadata": {}, 775 | "output_type": "execute_result" 776 | } 777 | ], 778 | "source": [ 779 | "# WE WILL LEARN A MUCH BETTER WAY TO DO THIS SOON!\n", 780 | "sum(banks['Closing Date'].apply(lambda date: date[-2:]) == '08')\n", 781 | "\n", 782 | "# Better way\n", 783 | "# sum(pd.to_datetime(banks['Closing Date']).apply(lambda date: date.year) == 2008)" 784 | ] 785 | }, 786 | { 787 | "cell_type": "markdown", 788 | "metadata": {}, 789 | "source": [ 790 | "# GREAT JOB!" 791 | ] 792 | } 793 | ], 794 | "metadata": { 795 | "kernelspec": { 796 | "display_name": "Python 3", 797 | "language": "python", 798 | "name": "python3" 799 | }, 800 | "language_info": { 801 | "codemirror_mode": { 802 | "name": "ipython", 803 | "version": 3 804 | }, 805 | "file_extension": ".py", 806 | "mimetype": "text/x-python", 807 | "name": "python", 808 | "nbconvert_exporter": "python", 809 | "pygments_lexer": "ipython3", 810 | "version": "3.6.1" 811 | } 812 | }, 813 | "nbformat": 4, 814 | "nbformat_minor": 2 815 | } 816 | -------------------------------------------------------------------------------- /03- General Pandas/Pandas-Exercises/Pandas Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Pandas Exercises" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Time to test your new pandas skills! Use the two csv files in this folder to complete the tasks in bold below!\n", 15 | "\n", 16 | "** NOTE: ALL TASKS MUST BE DONE IN ONE LINE OF PANDAS CODE. GOT STUCK? NO PROBLEM! CHECK OUT THE SOLUTIONS LECTURE! **" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "** Import pandas and read in the banklist.csv file into a dataframe called banks. **" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 25, 38 | "metadata": { 39 | "collapsed": true 40 | }, 41 | "outputs": [], 42 | "source": [] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "** Show the head of the dataframe **" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 26, 54 | "metadata": { 55 | "collapsed": true 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "# CODE HERE" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 37, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/html": [ 70 | "
\n", 71 | "\n", 84 | "\n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | "
Bank NameCitySTCERTAcquiring InstitutionClosing DateUpdated Date
0Fayette County BankSaint ElmoIL1802United Fidelity Bank, fsb26-May-171-Jun-17
1Guaranty Bank, (d/b/a BestBank in Georgia & Mi...MilwaukeeWI30003First-Citizens Bank & Trust Company5-May-171-Jun-17
2First NBC BankNew OrleansLA58302Whitney Bank28-Apr-1723-May-17
3Proficio BankCottonwood HeightsUT35495Cache Valley Bank3-Mar-1718-May-17
4Seaway Bank and Trust CompanyChicagoIL19328State Bank of Texas27-Jan-1718-May-17
\n", 150 | "
" 151 | ], 152 | "text/plain": [ 153 | " Bank Name City ST \\\n", 154 | "0 Fayette County Bank Saint Elmo IL \n", 155 | "1 Guaranty Bank, (d/b/a BestBank in Georgia & Mi... Milwaukee WI \n", 156 | "2 First NBC Bank New Orleans LA \n", 157 | "3 Proficio Bank Cottonwood Heights UT \n", 158 | "4 Seaway Bank and Trust Company Chicago IL \n", 159 | "\n", 160 | " CERT Acquiring Institution Closing Date Updated Date \n", 161 | "0 1802 United Fidelity Bank, fsb 26-May-17 1-Jun-17 \n", 162 | "1 30003 First-Citizens Bank & Trust Company 5-May-17 1-Jun-17 \n", 163 | "2 58302 Whitney Bank 28-Apr-17 23-May-17 \n", 164 | "3 35495 Cache Valley Bank 3-Mar-17 18-May-17 \n", 165 | "4 19328 State Bank of Texas 27-Jan-17 18-May-17 " 166 | ] 167 | }, 168 | "execution_count": 37, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "** What are the column names? **" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": { 186 | "collapsed": true 187 | }, 188 | "outputs": [], 189 | "source": [ 190 | "# CODE HERE" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 29, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "data": { 200 | "text/plain": [ 201 | "Index(['Bank Name', 'City', 'ST', 'CERT', 'Acquiring Institution',\n", 202 | " 'Closing Date', 'Updated Date'],\n", 203 | " dtype='object')" 204 | ] 205 | }, 206 | "execution_count": 29, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "** How many States (ST) are represented in this data set? **" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": { 224 | "collapsed": true 225 | }, 226 | "outputs": [], 227 | "source": [ 228 | "# CODE HERE" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 33, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "44" 240 | ] 241 | }, 242 | "execution_count": 33, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "** Get a list or array of all the states in the data set. **" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": { 260 | "collapsed": true 261 | }, 262 | "outputs": [], 263 | "source": [ 264 | "# CODE HERE" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 32, 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "data": { 274 | "text/plain": [ 275 | "array(['IL', 'WI', 'LA', 'UT', 'NJ', 'AR', 'GA', 'PA', 'TN', 'WA', 'CO',\n", 276 | " 'PR', 'FL', 'MN', 'CA', 'MD', 'OK', 'OH', 'SC', 'VA', 'ID', 'TX',\n", 277 | " 'CT', 'AZ', 'NV', 'NC', 'KY', 'MO', 'KS', 'AL', 'MI', 'IN', 'IA',\n", 278 | " 'NE', 'MS', 'NM', 'OR', 'NY', 'MA', 'SD', 'WY', 'WV', 'NH', 'HI'], dtype=object)" 279 | ] 280 | }, 281 | "execution_count": 32, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "** What are the top 5 states with the most failed banks? **" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": { 299 | "collapsed": true, 300 | "scrolled": true 301 | }, 302 | "outputs": [], 303 | "source": [ 304 | "# CODE HERE" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 35, 310 | "metadata": {}, 311 | "outputs": [ 312 | { 313 | "data": { 314 | "text/plain": [ 315 | "ST\n", 316 | "GA 93\n", 317 | "FL 75\n", 318 | "IL 67\n", 319 | "CA 41\n", 320 | "MN 23\n", 321 | "Name: Bank Name, dtype: int64" 322 | ] 323 | }, 324 | "execution_count": 35, 325 | "metadata": {}, 326 | "output_type": "execute_result" 327 | } 328 | ], 329 | "source": [] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "** What are the top 5 acquiring institutions? **" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": null, 341 | "metadata": { 342 | "collapsed": true 343 | }, 344 | "outputs": [], 345 | "source": [ 346 | "# CODE HERE" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 14, 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "data": { 356 | "text/plain": [ 357 | "No Acquirer 31\n", 358 | "State Bank and Trust Company 12\n", 359 | "First-Citizens Bank & Trust Company 11\n", 360 | "Ameris Bank 10\n", 361 | "U.S. Bank N.A. 9\n", 362 | "Name: Acquiring Institution, dtype: int64" 363 | ] 364 | }, 365 | "execution_count": 14, 366 | "metadata": {}, 367 | "output_type": "execute_result" 368 | } 369 | ], 370 | "source": [] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "** How many banks has the State Bank of Texas acquired? How many of them were actually in Texas?**" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": null, 382 | "metadata": { 383 | "collapsed": true 384 | }, 385 | "outputs": [], 386 | "source": [ 387 | "# CODE HERE" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": 15, 393 | "metadata": {}, 394 | "outputs": [ 395 | { 396 | "data": { 397 | "text/html": [ 398 | "
\n", 399 | "\n", 412 | "\n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | "
Bank NameCitySTCERTAcquiring InstitutionClosing DateUpdated Date
4Seaway Bank and Trust CompanyChicagoIL19328State Bank of Texas27-Jan-1718-May-17
21The National Republic Bank of ChicagoChicagoIL916State Bank of Texas24-Oct-146-Jan-16
450Millennium State Bank of TexasDallasTX57667State Bank of Texas2-Jul-0926-Oct-12
\n", 458 | "
" 459 | ], 460 | "text/plain": [ 461 | " Bank Name City ST CERT \\\n", 462 | "4 Seaway Bank and Trust Company Chicago IL 19328 \n", 463 | "21 The National Republic Bank of Chicago Chicago IL 916 \n", 464 | "450 Millennium State Bank of Texas Dallas TX 57667 \n", 465 | "\n", 466 | " Acquiring Institution Closing Date Updated Date \n", 467 | "4 State Bank of Texas 27-Jan-17 18-May-17 \n", 468 | "21 State Bank of Texas 24-Oct-14 6-Jan-16 \n", 469 | "450 State Bank of Texas 2-Jul-09 26-Oct-12 " 470 | ] 471 | }, 472 | "execution_count": 15, 473 | "metadata": {}, 474 | "output_type": "execute_result" 475 | } 476 | ], 477 | "source": [] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "** What is the most common city in California for a bank to fail in?**" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": null, 489 | "metadata": { 490 | "collapsed": true 491 | }, 492 | "outputs": [], 493 | "source": [ 494 | "# CODE HERE" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 24, 500 | "metadata": {}, 501 | "outputs": [ 502 | { 503 | "data": { 504 | "text/html": [ 505 | "
\n", 506 | "\n", 519 | "\n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | " \n", 531 | " \n", 532 | " \n", 533 | " \n", 534 | " \n", 535 | " \n", 536 | " \n", 537 | " \n", 538 | " \n", 539 | " \n", 540 | " \n", 541 | " \n", 542 | " \n", 543 | " \n", 544 | " \n", 545 | " \n", 546 | " \n", 547 | " \n", 548 | " \n", 549 | " \n", 550 | " \n", 551 | "
Bank NameSTCERTAcquiring InstitutionClosing DateUpdated Date
City
Los Angeles444444
\n", 552 | "
" 553 | ], 554 | "text/plain": [ 555 | " Bank Name ST CERT Acquiring Institution Closing Date \\\n", 556 | "City \n", 557 | "Los Angeles 4 4 4 4 4 \n", 558 | "\n", 559 | " Updated Date \n", 560 | "City \n", 561 | "Los Angeles 4 " 562 | ] 563 | }, 564 | "execution_count": 24, 565 | "metadata": {}, 566 | "output_type": "execute_result" 567 | } 568 | ], 569 | "source": [] 570 | }, 571 | { 572 | "cell_type": "markdown", 573 | "metadata": {}, 574 | "source": [ 575 | "** How many failed banks don't have the word \"Bank\" in their name? **" 576 | ] 577 | }, 578 | { 579 | "cell_type": "code", 580 | "execution_count": null, 581 | "metadata": { 582 | "collapsed": true 583 | }, 584 | "outputs": [], 585 | "source": [ 586 | "# CODE HERE" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 55, 592 | "metadata": {}, 593 | "outputs": [ 594 | { 595 | "data": { 596 | "text/plain": [ 597 | "14" 598 | ] 599 | }, 600 | "execution_count": 55, 601 | "metadata": {}, 602 | "output_type": "execute_result" 603 | } 604 | ], 605 | "source": [] 606 | }, 607 | { 608 | "cell_type": "markdown", 609 | "metadata": {}, 610 | "source": [ 611 | "** How many bank names start with the letter 's' ? **" 612 | ] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "execution_count": null, 617 | "metadata": { 618 | "collapsed": true 619 | }, 620 | "outputs": [], 621 | "source": [ 622 | "# CODE HERE" 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": 58, 628 | "metadata": {}, 629 | "outputs": [ 630 | { 631 | "data": { 632 | "text/plain": [ 633 | "53" 634 | ] 635 | }, 636 | "execution_count": 58, 637 | "metadata": {}, 638 | "output_type": "execute_result" 639 | } 640 | ], 641 | "source": [] 642 | }, 643 | { 644 | "cell_type": "markdown", 645 | "metadata": {}, 646 | "source": [ 647 | "** How many CERT values are above 20000 ? **" 648 | ] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": 61, 653 | "metadata": { 654 | "collapsed": true 655 | }, 656 | "outputs": [], 657 | "source": [ 658 | "# CODE HERE" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 64, 664 | "metadata": {}, 665 | "outputs": [ 666 | { 667 | "data": { 668 | "text/plain": [ 669 | "417" 670 | ] 671 | }, 672 | "execution_count": 64, 673 | "metadata": {}, 674 | "output_type": "execute_result" 675 | } 676 | ], 677 | "source": [] 678 | }, 679 | { 680 | "cell_type": "markdown", 681 | "metadata": {}, 682 | "source": [ 683 | "** How many bank names consist of just two words? (e.g. \"First Bank\" , \"Bank Georgia\" )**" 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": 65, 689 | "metadata": { 690 | "collapsed": true 691 | }, 692 | "outputs": [], 693 | "source": [ 694 | "# CODE HERE" 695 | ] 696 | }, 697 | { 698 | "cell_type": "code", 699 | "execution_count": 67, 700 | "metadata": {}, 701 | "outputs": [ 702 | { 703 | "data": { 704 | "text/plain": [ 705 | "114" 706 | ] 707 | }, 708 | "execution_count": 67, 709 | "metadata": {}, 710 | "output_type": "execute_result" 711 | } 712 | ], 713 | "source": [] 714 | }, 715 | { 716 | "cell_type": "markdown", 717 | "metadata": {}, 718 | "source": [ 719 | "**Bonus: How many banks closed in the year 2008? (this is hard because we technically haven't learned about time series with pandas yet! Feel free to skip this one!**" 720 | ] 721 | }, 722 | { 723 | "cell_type": "code", 724 | "execution_count": null, 725 | "metadata": { 726 | "collapsed": true 727 | }, 728 | "outputs": [], 729 | "source": [ 730 | "# CODE HERE" 731 | ] 732 | }, 733 | { 734 | "cell_type": "code", 735 | "execution_count": 54, 736 | "metadata": {}, 737 | "outputs": [ 738 | { 739 | "data": { 740 | "text/plain": [ 741 | "25" 742 | ] 743 | }, 744 | "execution_count": 54, 745 | "metadata": {}, 746 | "output_type": "execute_result" 747 | } 748 | ], 749 | "source": [] 750 | }, 751 | { 752 | "cell_type": "markdown", 753 | "metadata": {}, 754 | "source": [ 755 | "# GREAT JOB!" 756 | ] 757 | } 758 | ], 759 | "metadata": { 760 | "kernelspec": { 761 | "display_name": "Python 3", 762 | "language": "python", 763 | "name": "python3" 764 | }, 765 | "language_info": { 766 | "codemirror_mode": { 767 | "name": "ipython", 768 | "version": 3 769 | }, 770 | "file_extension": ".py", 771 | "mimetype": "text/x-python", 772 | "name": "python", 773 | "nbconvert_exporter": "python", 774 | "pygments_lexer": "ipython3", 775 | "version": "3.6.1" 776 | } 777 | }, 778 | "nbformat": 4, 779 | "nbformat_minor": 2 780 | } 781 | -------------------------------------------------------------------------------- /03- General Pandas/example: -------------------------------------------------------------------------------- 1 | a,b,c,d 2 | 0,1,2,3 3 | 4,5,6,7 4 | 8,9,10,11 5 | 12,13,14,15 6 | -------------------------------------------------------------------------------- /03- General Pandas/multi_index_example: -------------------------------------------------------------------------------- 1 | first,bar,bar,baz,baz,foo,foo,qux,qux 2 | second,one,two,one,two,one,two,one,two 3 | ,,,,,,,, 4 | A,1.025984152081572,-0.1565979042889875,-0.031579143908112575,0.6498258334908454,2.154846443259472,-0.6102588558227414,-0.755325340010558,-0.34641850351854453 5 | B,0.1470267713241236,-0.47944803904109595,0.558769406443067,1.0248102783372157,-0.925874258809907,1.8628641384939535,-1.1338171615837889,0.6104779075384634 6 | C,0.3860303121135517,2.084018530338962,-0.37651867524923904,0.23033634359240704,0.6812092925867574,1.0351250747739213,-0.031160481493099617,1.9399323109926203 7 | -------------------------------------------------------------------------------- /04-Visualization-Matplotlib-Pandas/04-01-Matplotlib/filename.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BessieChen/Python-for-Financial-Analysis-and-Algorithmic-Trading/849c322ccaffa10433f2f09e53f5907fc21fb0d7/04-Visualization-Matplotlib-Pandas/04-01-Matplotlib/filename.png -------------------------------------------------------------------------------- /04-Visualization-Matplotlib-Pandas/04-02-Pandas Visualization/df2: -------------------------------------------------------------------------------- 1 | a,b,c,d 2 | 0.039761986133905136,0.2185172274750622,0.10342298051665423,0.9579042338107532 3 | 0.9372879037285884,0.04156728027953449,0.8991254222382951,0.9776795571253272 4 | 0.7805044779316328,0.008947537857148302,0.5578084027546968,0.7975104497549266 5 | 0.6727174963492204,0.24786984946279625,0.2640713103088026,0.44435791644122935 6 | 0.05382860859967886,0.5201244020579979,0.5522642392797277,0.19000759632053632 7 | 0.2860433671280178,0.5934650440000543,0.9073072637456548,0.6378977150631427 8 | 0.4304355863327313,0.16623013749421356,0.4693825447762464,0.4977008828313123 9 | 0.3122955538295512,0.5028232900921878,0.8066087010958843,0.8505190941429479 10 | 0.1877648514121828,0.9970746427719338,0.8959552961495315,0.530390137569463 11 | 0.9081621790575398,0.23272641071536715,0.4141382611943452,0.4320069001558664 12 | -------------------------------------------------------------------------------- /05-Pandas-with-Time-Series/01-M-Datetime Index.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "___\n", 8 | "\n", 9 | " \n", 10 | "___\n", 11 | "
*Copyright Pierian Data 2017*
\n", 12 | "
*For more information, visit us at www.pieriandata.com*
" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "# Introduction to Time Series with Pandas\n", 20 | "\n", 21 | "A lot of our financial data will have a datatime index, so let's learn how to deal with this sort of data with pandas!" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 15, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "import numpy as np\n", 33 | "import pandas as pd\n", 34 | "import matplotlib.pyplot as plt" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": { 41 | "collapsed": true 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "from datetime import datetime" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "# To illustrate the order of arguments\n", 57 | "my_year = 2017\n", 58 | "my_month = 1\n", 59 | "my_day = 2\n", 60 | "my_hour = 13\n", 61 | "my_minute = 30\n", 62 | "my_second = 15" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 4, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# January 2nd, 2017\n", 72 | "my_date = datetime(my_year,my_month,my_day)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "datetime.datetime(2017, 1, 2, 0, 0)" 84 | ] 85 | }, 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "# Defaults to 0:00\n", 93 | "my_date " 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 6, 99 | "metadata": { 100 | "collapsed": true 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "# January 2nd, 2017 at 13:30:15\n", 105 | "my_date_time = datetime(my_year,my_month,my_day,my_hour,my_minute,my_second)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 7, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "datetime.datetime(2017, 1, 2, 13, 30, 15)" 117 | ] 118 | }, 119 | "execution_count": 7, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "my_date_time" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "You can grab any part of the datetime object you want" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 8, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "2" 144 | ] 145 | }, 146 | "execution_count": 8, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "my_date.day" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 9, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "13" 164 | ] 165 | }, 166 | "execution_count": 9, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "my_date_time.hour" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "### Pandas with Datetime Index\n", 180 | "\n", 181 | "You'll usually deal with time series as an index when working with pandas dataframes obtained from some sort of financial API. Fortunately pandas has a lot of functions and methods to work with time series!" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 13, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "[datetime.datetime(2016, 1, 1, 0, 0), datetime.datetime(2016, 1, 2, 0, 0)]" 193 | ] 194 | }, 195 | "execution_count": 13, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "# Create an example datetime list/array\n", 202 | "first_two = [datetime(2016, 1, 1), datetime(2016, 1, 2)]\n", 203 | "first_two" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 14, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "DatetimeIndex(['2016-01-01', '2016-01-02'], dtype='datetime64[ns]', freq=None)" 215 | ] 216 | }, 217 | "execution_count": 14, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "# Converted to an index\n", 224 | "dt_ind = pd.DatetimeIndex(first_two)\n", 225 | "dt_ind" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 29, 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "name": "stdout", 235 | "output_type": "stream", 236 | "text": [ 237 | "[[-1.58270607 0.47766839]\n", 238 | " [ 0.34171008 0.5889566 ]]\n" 239 | ] 240 | } 241 | ], 242 | "source": [ 243 | "# Attached to some random data\n", 244 | "data = np.random.randn(2,2)\n", 245 | "print(data)\n", 246 | "cols = ['A','B']" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 28, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [ 255 | "df = pd.DataFrame(data,dt_ind,cols)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 30, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/html": [ 266 | "
\n", 267 | "\n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | "
AB
2016-01-010.165224-0.767629
2016-01-02-0.4823050.307934
\n", 288 | "
" 289 | ], 290 | "text/plain": [ 291 | " A B\n", 292 | "2016-01-01 0.165224 -0.767629\n", 293 | "2016-01-02 -0.482305 0.307934" 294 | ] 295 | }, 296 | "execution_count": 30, 297 | "metadata": {}, 298 | "output_type": "execute_result" 299 | } 300 | ], 301 | "source": [ 302 | "df" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 31, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "DatetimeIndex(['2016-01-01', '2016-01-02'], dtype='datetime64[ns]', freq=None)" 314 | ] 315 | }, 316 | "execution_count": 31, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "df.index" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 34, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "1" 334 | ] 335 | }, 336 | "execution_count": 34, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "# Latest Date Location\n", 343 | "df.index.argmax()" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 35, 349 | "metadata": {}, 350 | "outputs": [ 351 | { 352 | "data": { 353 | "text/plain": [ 354 | "Timestamp('2016-01-02 00:00:00')" 355 | ] 356 | }, 357 | "execution_count": 35, 358 | "metadata": {}, 359 | "output_type": "execute_result" 360 | } 361 | ], 362 | "source": [ 363 | "df.index.max()" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 37, 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "data": { 373 | "text/plain": [ 374 | "0" 375 | ] 376 | }, 377 | "execution_count": 37, 378 | "metadata": {}, 379 | "output_type": "execute_result" 380 | } 381 | ], 382 | "source": [ 383 | "# Earliest Date Index Location\n", 384 | "df.index.argmin()" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 38, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "data": { 394 | "text/plain": [ 395 | "Timestamp('2016-01-01 00:00:00')" 396 | ] 397 | }, 398 | "execution_count": 38, 399 | "metadata": {}, 400 | "output_type": "execute_result" 401 | } 402 | ], 403 | "source": [ 404 | "df.index.min()" 405 | ] 406 | }, 407 | { 408 | "cell_type": "markdown", 409 | "metadata": {}, 410 | "source": [ 411 | "## Great, let's move on!" 412 | ] 413 | } 414 | ], 415 | "metadata": { 416 | "anaconda-cloud": {}, 417 | "kernelspec": { 418 | "display_name": "Python 2", 419 | "language": "python", 420 | "name": "python2" 421 | }, 422 | "language_info": { 423 | "codemirror_mode": { 424 | "name": "ipython", 425 | "version": 2 426 | }, 427 | "file_extension": ".py", 428 | "mimetype": "text/x-python", 429 | "name": "python", 430 | "nbconvert_exporter": "python", 431 | "pygments_lexer": "ipython2", 432 | "version": "2.7.14" 433 | } 434 | }, 435 | "nbformat": 4, 436 | "nbformat_minor": 1 437 | } 438 | -------------------------------------------------------------------------------- /08-Time-Series-Analysis/airline_passengers.csv: -------------------------------------------------------------------------------- 1 | "Month","Thousands of Passengers" 2 | "1949-01",112 3 | "1949-02",118 4 | "1949-03",132 5 | "1949-04",129 6 | "1949-05",121 7 | "1949-06",135 8 | "1949-07",148 9 | "1949-08",148 10 | "1949-09",136 11 | "1949-10",119 12 | "1949-11",104 13 | "1949-12",118 14 | "1950-01",115 15 | "1950-02",126 16 | "1950-03",141 17 | "1950-04",135 18 | "1950-05",125 19 | "1950-06",149 20 | "1950-07",170 21 | "1950-08",170 22 | "1950-09",158 23 | "1950-10",133 24 | "1950-11",114 25 | "1950-12",140 26 | "1951-01",145 27 | "1951-02",150 28 | "1951-03",178 29 | "1951-04",163 30 | "1951-05",172 31 | "1951-06",178 32 | "1951-07",199 33 | "1951-08",199 34 | "1951-09",184 35 | "1951-10",162 36 | "1951-11",146 37 | "1951-12",166 38 | "1952-01",171 39 | "1952-02",180 40 | "1952-03",193 41 | "1952-04",181 42 | "1952-05",183 43 | "1952-06",218 44 | "1952-07",230 45 | "1952-08",242 46 | "1952-09",209 47 | "1952-10",191 48 | "1952-11",172 49 | "1952-12",194 50 | "1953-01",196 51 | "1953-02",196 52 | "1953-03",236 53 | "1953-04",235 54 | "1953-05",229 55 | "1953-06",243 56 | "1953-07",264 57 | "1953-08",272 58 | "1953-09",237 59 | "1953-10",211 60 | "1953-11",180 61 | "1953-12",201 62 | "1954-01",204 63 | "1954-02",188 64 | "1954-03",235 65 | "1954-04",227 66 | "1954-05",234 67 | "1954-06",264 68 | "1954-07",302 69 | "1954-08",293 70 | "1954-09",259 71 | "1954-10",229 72 | "1954-11",203 73 | "1954-12",229 74 | "1955-01",242 75 | "1955-02",233 76 | "1955-03",267 77 | "1955-04",269 78 | "1955-05",270 79 | "1955-06",315 80 | "1955-07",364 81 | "1955-08",347 82 | "1955-09",312 83 | "1955-10",274 84 | "1955-11",237 85 | "1955-12",278 86 | "1956-01",284 87 | "1956-02",277 88 | "1956-03",317 89 | "1956-04",313 90 | "1956-05",318 91 | "1956-06",374 92 | "1956-07",413 93 | "1956-08",405 94 | "1956-09",355 95 | "1956-10",306 96 | "1956-11",271 97 | "1956-12",306 98 | "1957-01",315 99 | "1957-02",301 100 | "1957-03",356 101 | "1957-04",348 102 | "1957-05",355 103 | "1957-06",422 104 | "1957-07",465 105 | "1957-08",467 106 | "1957-09",404 107 | "1957-10",347 108 | "1957-11",305 109 | "1957-12",336 110 | "1958-01",340 111 | "1958-02",318 112 | "1958-03",362 113 | "1958-04",348 114 | "1958-05",363 115 | "1958-06",435 116 | "1958-07",491 117 | "1958-08",505 118 | "1958-09",404 119 | "1958-10",359 120 | "1958-11",310 121 | "1958-12",337 122 | "1959-01",360 123 | "1959-02",342 124 | "1959-03",406 125 | "1959-04",396 126 | "1959-05",420 127 | "1959-06",472 128 | "1959-07",548 129 | "1959-08",559 130 | "1959-09",463 131 | "1959-10",407 132 | "1959-11",362 133 | "1959-12",405 134 | "1960-01",417 135 | "1960-02",391 136 | "1960-03",419 137 | "1960-04",461 138 | "1960-05",472 139 | "1960-06",535 140 | "1960-07",622 141 | "1960-08",606 142 | "1960-09",508 143 | "1960-10",461 144 | "1960-11",390 145 | "1960-12",432 146 | 147 | International airline passengers: monthly totals in thousands. Jan 49 ? Dec 60 148 | 149 | -------------------------------------------------------------------------------- /08-Time-Series-Analysis/monthly-milk-production-pounds-p.csv: -------------------------------------------------------------------------------- 1 | "Month","Monthly milk production: pounds per cow. Jan 62 ? Dec 75" 2 | "1962-01",589 3 | "1962-02",561 4 | "1962-03",640 5 | "1962-04",656 6 | "1962-05",727 7 | "1962-06",697 8 | "1962-07",640 9 | "1962-08",599 10 | "1962-09",568 11 | "1962-10",577 12 | "1962-11",553 13 | "1962-12",582 14 | "1963-01",600 15 | "1963-02",566 16 | "1963-03",653 17 | "1963-04",673 18 | "1963-05",742 19 | "1963-06",716 20 | "1963-07",660 21 | "1963-08",617 22 | "1963-09",583 23 | "1963-10",587 24 | "1963-11",565 25 | "1963-12",598 26 | "1964-01",628 27 | "1964-02",618 28 | "1964-03",688 29 | "1964-04",705 30 | "1964-05",770 31 | "1964-06",736 32 | "1964-07",678 33 | "1964-08",639 34 | "1964-09",604 35 | "1964-10",611 36 | "1964-11",594 37 | "1964-12",634 38 | "1965-01",658 39 | "1965-02",622 40 | "1965-03",709 41 | "1965-04",722 42 | "1965-05",782 43 | "1965-06",756 44 | "1965-07",702 45 | "1965-08",653 46 | "1965-09",615 47 | "1965-10",621 48 | "1965-11",602 49 | "1965-12",635 50 | "1966-01",677 51 | "1966-02",635 52 | "1966-03",736 53 | "1966-04",755 54 | "1966-05",811 55 | "1966-06",798 56 | "1966-07",735 57 | "1966-08",697 58 | "1966-09",661 59 | "1966-10",667 60 | "1966-11",645 61 | "1966-12",688 62 | "1967-01",713 63 | "1967-02",667 64 | "1967-03",762 65 | "1967-04",784 66 | "1967-05",837 67 | "1967-06",817 68 | "1967-07",767 69 | "1967-08",722 70 | "1967-09",681 71 | "1967-10",687 72 | "1967-11",660 73 | "1967-12",698 74 | "1968-01",717 75 | "1968-02",696 76 | "1968-03",775 77 | "1968-04",796 78 | "1968-05",858 79 | "1968-06",826 80 | "1968-07",783 81 | "1968-08",740 82 | "1968-09",701 83 | "1968-10",706 84 | "1968-11",677 85 | "1968-12",711 86 | "1969-01",734 87 | "1969-02",690 88 | "1969-03",785 89 | "1969-04",805 90 | "1969-05",871 91 | "1969-06",845 92 | "1969-07",801 93 | "1969-08",764 94 | "1969-09",725 95 | "1969-10",723 96 | "1969-11",690 97 | "1969-12",734 98 | "1970-01",750 99 | "1970-02",707 100 | "1970-03",807 101 | "1970-04",824 102 | "1970-05",886 103 | "1970-06",859 104 | "1970-07",819 105 | "1970-08",783 106 | "1970-09",740 107 | "1970-10",747 108 | "1970-11",711 109 | "1970-12",751 110 | "1971-01",804 111 | "1971-02",756 112 | "1971-03",860 113 | "1971-04",878 114 | "1971-05",942 115 | "1971-06",913 116 | "1971-07",869 117 | "1971-08",834 118 | "1971-09",790 119 | "1971-10",800 120 | "1971-11",763 121 | "1971-12",800 122 | "1972-01",826 123 | "1972-02",799 124 | "1972-03",890 125 | "1972-04",900 126 | "1972-05",961 127 | "1972-06",935 128 | "1972-07",894 129 | "1972-08",855 130 | "1972-09",809 131 | "1972-10",810 132 | "1972-11",766 133 | "1972-12",805 134 | "1973-01",821 135 | "1973-02",773 136 | "1973-03",883 137 | "1973-04",898 138 | "1973-05",957 139 | "1973-06",924 140 | "1973-07",881 141 | "1973-08",837 142 | "1973-09",784 143 | "1973-10",791 144 | "1973-11",760 145 | "1973-12",802 146 | "1974-01",828 147 | "1974-02",778 148 | "1974-03",889 149 | "1974-04",902 150 | "1974-05",969 151 | "1974-06",947 152 | "1974-07",908 153 | "1974-08",867 154 | "1974-09",815 155 | "1974-10",812 156 | "1974-11",773 157 | "1974-12",813 158 | "1975-01",834 159 | "1975-02",782 160 | "1975-03",892 161 | "1975-04",903 162 | "1975-05",966 163 | "1975-06",937 164 | "1975-07",896 165 | "1975-08",858 166 | "1975-09",817 167 | "1975-10",827 168 | "1975-11",797 169 | "1975-12",843 170 | 171 | Monthly milk production: pounds per cow. Jan 62 ? Dec 75 172 | 173 | -------------------------------------------------------------------------------- /09-Python-Finance-Fundamentals/AAPL_CLOSE: -------------------------------------------------------------------------------- 1 | Date,Adj. Close 2 | 2017-03-06,138.21132621523 3 | 2017-03-07,138.38986818967 4 | 2017-03-08,137.87408026351 5 | 2017-03-09,137.55667230894 6 | 2017-03-10,138.01294624363 7 | 2017-03-13,138.07246023511 8 | 2017-03-14,137.86416126493 9 | 2017-03-15,139.3222540562 10 | 2017-03-16,139.55039102354 11 | 2017-03-17,138.85606112294 12 | 2017-03-20,140.31415391421 13 | 2017-03-21,138.70727614424 14 | 2017-03-22,140.27447791989 15 | 2017-03-23,139.77852799089 16 | 2017-03-24,139.50079603064 17 | 2017-03-27,139.73885199657 18 | 2017-03-28,142.63519958196 19 | 2017-03-29,142.95260753652 20 | 2017-03-30,142.7641465635 21 | 2017-03-31,142.49633360184 22 | 2017-04-03,142.53600959616 23 | 2017-04-04,143.59734244423 24 | 2017-04-05,142.85341755072 25 | 2017-04-06,142.49633360184 26 | 2017-04-07,142.17892564727 27 | 2017-04-10,142.01030267141 28 | 2017-04-11,140.48277689007 29 | 2017-04-12,140.65139986594 30 | 2017-04-13,139.90747497243 31 | 2017-04-17,140.68115686168 32 | 2017-04-18,140.05625995113 33 | 2017-04-19,139.54047202496 34 | 2017-04-20,141.28621577506 35 | 2017-04-21,141.1175927992 36 | 2017-04-24,142.47649560468 37 | 2017-04-25,143.36920547689 38 | 2017-04-26,142.48720812314 39 | 2017-04-27,142.62528058338 40 | 2017-04-28,142.48641460326 41 | 2017-05-01,145.41251918439 42 | 2017-05-02,146.31514805518 43 | 2017-05-03,145.86879311907 44 | 2017-05-04,145.34308619433 45 | 2017-05-05,147.75340284929 46 | 2017-05-08,151.76067827566 47 | 2017-05-09,152.71290213935 48 | 2017-05-10,152.01857223874 49 | 2017-05-11,153.32788005131 50 | 2017-05-12,155.46919178961 51 | 2017-05-15,155.07080821039 52 | 2017-05-16,154.84173765234 53 | 2017-05-17,149.64283194355 54 | 2017-05-18,151.92357793457 55 | 2017-05-19,152.34188069275 56 | 2017-05-22,153.36771840924 57 | 2017-05-23,153.17848620911 58 | 2017-05-24,152.72034509301 59 | 2017-05-25,153.24820333547 60 | 2017-05-26,152.98925400898 61 | 2017-05-30,153.04901154586 62 | 2017-05-31,152.14268890314 63 | 2017-06-01,152.56099166132 64 | 2017-06-02,154.82181847338 65 | 2017-06-05,153.30796087235 66 | 2017-06-06,153.82585952534 67 | 2017-06-07,154.74214175754 68 | 2017-06-08,154.36367735728 69 | 2017-06-09,148.37796407954 70 | 2017-06-12,144.7327543297 71 | 2017-06-13,145.99762219371 72 | 2017-06-14,144.57340089801 73 | 2017-06-15,143.70691661321 74 | 2017-06-16,141.69507953817 75 | 2017-06-19,145.7486324567 76 | 2017-06-20,144.4240070558 77 | 2017-06-21,145.28053175112 78 | 2017-06-22,145.04150160359 79 | 2017-06-23,145.75859204618 80 | 2017-06-26,145.23073380372 81 | 2017-06-27,143.15913919179 82 | 2017-06-28,145.2406933932 83 | 2017-06-29,143.09938165491 84 | 2017-06-30,143.43800769724 85 | 2017-07-03,142.92010904426 86 | 2017-07-05,143.5077248236 87 | 2017-07-06,142.15322065427 88 | 2017-07-07,143.59736112893 89 | 2017-07-10,144.47380500321 90 | 2017-07-11,144.94190570879 91 | 2017-07-12,145.15105708788 92 | 2017-07-13,147.17285375241 93 | 2017-07-14,148.43772161642 94 | 2017-07-17,148.9556202694 95 | 2017-07-18,149.47351892239 96 | 2017-07-19,150.40972033355 97 | 2017-07-20,149.73246824888 98 | 2017-07-21,149.66275112251 99 | 2017-07-24,151.47539640795 100 | 2017-07-25,152.12276972418 101 | 2017-07-26,152.83986016677 102 | 2017-07-27,149.95157921745 103 | 2017-07-28,148.89586273252 104 | 2017-07-31,148.24848941629 105 | 2017-08-01,149.44364015394 106 | 2017-08-02,156.50498909557 107 | 2017-08-03,154.94133354715 108 | 2017-08-04,155.75801988454 109 | 2017-08-08,159.43310840282 110 | 2017-08-09,160.40914817191 111 | 2017-08-10,155.27 112 | 2017-08-11,157.48 113 | 2017-08-14,159.85 114 | 2017-08-15,161.6 115 | 2017-08-16,160.95 116 | 2017-08-17,157.87 117 | 2017-08-18,157.5 118 | 2017-08-21,157.21 119 | 2017-08-22,159.78 120 | 2017-08-23,159.98 121 | 2017-08-24,159.27 122 | 2017-08-25,159.86 123 | 2017-08-28,161.47 124 | 2017-08-29,162.91 125 | 2017-08-30,163.35 126 | 2017-08-31,164.0 127 | 2017-09-01,164.05 128 | 2017-09-05,162.08 129 | 2017-09-06,161.91 130 | 2017-09-07,161.26 131 | -------------------------------------------------------------------------------- /09-Python-Finance-Fundamentals/AMZN_CLOSE: -------------------------------------------------------------------------------- 1 | Date,Adj. Close 2 | 2017-03-06,846.61 3 | 2017-03-07,846.02 4 | 2017-03-08,850.5 5 | 2017-03-09,853.0 6 | 2017-03-10,852.46 7 | 2017-03-13,854.59 8 | 2017-03-14,852.53 9 | 2017-03-15,852.97 10 | 2017-03-16,853.42 11 | 2017-03-17,852.31 12 | 2017-03-20,856.97 13 | 2017-03-21,843.2 14 | 2017-03-22,848.06 15 | 2017-03-23,847.38 16 | 2017-03-24,845.61 17 | 2017-03-27,846.82 18 | 2017-03-28,856.0 19 | 2017-03-29,874.32 20 | 2017-03-30,876.34 21 | 2017-03-31,886.54 22 | 2017-04-03,891.51 23 | 2017-04-04,906.83 24 | 2017-04-05,909.28 25 | 2017-04-06,898.28 26 | 2017-04-07,894.88 27 | 2017-04-10,907.04 28 | 2017-04-11,902.36 29 | 2017-04-12,896.23 30 | 2017-04-13,884.67 31 | 2017-04-17,901.99 32 | 2017-04-18,903.78 33 | 2017-04-19,899.2 34 | 2017-04-20,902.06 35 | 2017-04-21,898.53 36 | 2017-04-24,907.41 37 | 2017-04-25,907.62 38 | 2017-04-26,909.29 39 | 2017-04-27,918.76 40 | 2017-04-28,924.99 41 | 2017-05-01,948.43 42 | 2017-05-02,946.68 43 | 2017-05-03,941.03 44 | 2017-05-04,937.53 45 | 2017-05-05,934.15 46 | 2017-05-08,949.04 47 | 2017-05-09,952.82 48 | 2017-05-10,948.95 49 | 2017-05-11,947.62 50 | 2017-05-12,961.35 51 | 2017-05-15,957.97 52 | 2017-05-16,966.07 53 | 2017-05-17,944.52 54 | 2017-05-18,958.49 55 | 2017-05-19,959.84 56 | 2017-05-22,970.7 57 | 2017-05-23,971.54 58 | 2017-05-24,980.35 59 | 2017-05-25,993.38 60 | 2017-05-26,995.78 61 | 2017-05-30,996.7 62 | 2017-05-31,994.62 63 | 2017-06-01,995.95 64 | 2017-06-02,1006.73 65 | 2017-06-05,1011.34 66 | 2017-06-06,1002.97 67 | 2017-06-07,1010.07 68 | 2017-06-08,1009.94 69 | 2017-06-09,978.31 70 | 2017-06-12,964.83 71 | 2017-06-13,980.79 72 | 2017-06-14,976.47 73 | 2017-06-15,964.17 74 | 2017-06-16,987.71 75 | 2017-06-19,995.17 76 | 2017-06-20,992.59 77 | 2017-06-21,1002.23 78 | 2017-06-22,1001.3 79 | 2017-06-23,1003.74 80 | 2017-06-26,993.98 81 | 2017-06-27,976.78 82 | 2017-06-28,990.33 83 | 2017-06-29,975.93 84 | 2017-06-30,968.0 85 | 2017-07-03,953.66 86 | 2017-07-05,971.4 87 | 2017-07-06,965.14 88 | 2017-07-07,978.76 89 | 2017-07-10,996.47 90 | 2017-07-11,994.13 91 | 2017-07-12,1006.51 92 | 2017-07-13,999.855 93 | 2017-07-14,1001.81 94 | 2017-07-17,1010.04 95 | 2017-07-18,1024.38 96 | 2017-07-19,1026.87 97 | 2017-07-20,1028.7 98 | 2017-07-21,1025.67 99 | 2017-07-24,1038.95 100 | 2017-07-25,1039.87 101 | 2017-07-26,1052.8 102 | 2017-07-27,1046.0 103 | 2017-07-28,1020.04 104 | 2017-07-31,987.78 105 | 2017-08-01,996.19 106 | 2017-08-02,995.89 107 | 2017-08-03,986.92 108 | 2017-08-04,987.58 109 | 2017-08-08,989.84 110 | 2017-08-09,982.01 111 | 2017-08-10,956.92 112 | 2017-08-11,967.99 113 | 2017-08-14,983.3 114 | 2017-08-15,982.74 115 | 2017-08-16,978.18 116 | 2017-08-17,960.57 117 | 2017-08-18,958.47 118 | 2017-08-21,953.29 119 | 2017-08-22,966.9 120 | 2017-08-23,958.0 121 | 2017-08-24,952.45 122 | 2017-08-25,945.26 123 | 2017-08-28,946.02 124 | 2017-08-29,954.06 125 | 2017-08-30,967.59 126 | 2017-08-31,980.6 127 | 2017-09-01,978.25 128 | 2017-09-05,965.27 129 | 2017-09-06,967.8 130 | 2017-09-07,979.47 131 | -------------------------------------------------------------------------------- /09-Python-Finance-Fundamentals/CISCO_CLOSE: -------------------------------------------------------------------------------- 1 | Date,Adj. Close 2 | 2017-03-06,33.293999308418 3 | 2017-03-07,33.303737243284 4 | 2017-03-08,33.128454415688 5 | 2017-03-09,33.17714409002 6 | 2017-03-10,33.362164852483 7 | 2017-03-13,33.20635789462 8 | 2017-03-14,33.225833764352 9 | 2017-03-15,33.34268898275 10 | 2017-03-16,33.332951047884 11 | 2017-03-17,33.332951047884 12 | 2017-03-20,33.381640722216 13 | 2017-03-21,32.992123327558 14 | 2017-03-22,33.20635789462 15 | 2017-03-23,33.070026806489 16 | 2017-03-24,33.186882024887 17 | 2017-03-27,33.099240611089 18 | 2017-03-28,33.128454415688 19 | 2017-03-29,32.855792239427 20 | 2017-03-30,32.855792239427 21 | 2017-03-31,32.914219848626 22 | 2017-04-03,32.699985281564 23 | 2017-04-04,32.816840499961 24 | 2017-04-05,32.414119619836 25 | 2017-04-06,32.492699303763 26 | 2017-04-07,32.374829777873 27 | 2017-04-10,32.423942080327 28 | 2017-04-11,32.335539935909 29 | 2017-04-12,32.040866121183 30 | 2017-04-13,31.844416911366 31 | 2017-04-17,32.031043660693 32 | 2017-04-18,32.089978423638 33 | 2017-04-19,32.070333502656 34 | 2017-04-20,32.247137791491 35 | 2017-04-21,32.237315331001 36 | 2017-04-24,32.68914851358 37 | 2017-04-25,32.826662960452 38 | 2017-04-26,32.80701803947 39 | 2017-04-27,33.150804156651 40 | 2017-04-28,33.455300431867 41 | 2017-05-01,33.361987057204 42 | 2017-05-02,33.622282260212 43 | 2017-05-03,33.641927181193 44 | 2017-05-04,33.573169957757 45 | 2017-05-05,33.779441628066 46 | 2017-05-08,33.681217023157 47 | 2017-05-09,33.298141064013 48 | 2017-05-10,33.14098169616 49 | 2017-05-11,33.03293463076 50 | 2017-05-12,32.856130341925 51 | 2017-05-15,33.622282260212 52 | 2017-05-16,33.691039483648 53 | 2017-05-17,33.229383840577 54 | 2017-05-18,30.822881020317 55 | 2017-05-19,30.655899191972 56 | 2017-05-22,31.029152690625 57 | 2017-05-23,31.196134518969 58 | 2017-05-24,30.930928085716 59 | 2017-05-25,30.881815783262 60 | 2017-05-26,30.940750546207 61 | 2017-05-30,31.117554835043 62 | 2017-05-31,30.97021792768 63 | 2017-06-01,31.255069281915 64 | 2017-06-02,31.412228649768 65 | 2017-06-05,31.196134518969 66 | 2017-06-06,30.999685309152 67 | 2017-06-07,31.048797611607 68 | 2017-06-08,31.053708841852 69 | 2017-06-09,30.813058559826 70 | 2017-06-12,30.69027780369 71 | 2017-06-13,31.137199756024 72 | 2017-06-14,31.038975151116 73 | 2017-06-15,31.019428454739 74 | 2017-06-16,31.058718296702 75 | 2017-06-19,31.422051110259 76 | 2017-06-20,31.284536663387 77 | 2017-06-21,31.274714202896 78 | 2017-06-22,31.284536663387 79 | 2017-06-23,31.530098175659 80 | 2017-06-26,31.667612622531 81 | 2017-06-27,31.196134518969 82 | 2017-06-28,31.510453254677 83 | 2017-06-29,30.852348401789 84 | 2017-06-30,30.74430133639 85 | 2017-07-03,30.773768717863 86 | 2017-07-05,30.842525941299 87 | 2017-07-06,30.460836050993 88 | 2017-07-07,30.634331455677 89 | 2017-07-10,30.713643640676 90 | 2017-07-11,30.822697895049 91 | 2017-07-12,30.892096056923 92 | 2017-07-13,31.001150311296 93 | 2017-07-14,31.149860658168 94 | 2017-07-17,31.229172843166 95 | 2017-07-18,31.239086866291 96 | 2017-07-19,31.625733768159 97 | 2017-07-20,31.58607767566 98 | 2017-07-21,31.56624962941 99 | 2017-07-24,31.58607767566 100 | 2017-07-25,31.843842276905 101 | 2017-07-26,31.387797213163 102 | 2017-07-27,31.29857100504 103 | 2017-07-28,31.249000889416 104 | 2017-07-31,31.179602727542 105 | 2017-08-01,31.377883190039 106 | 2017-08-02,31.249000889416 107 | 2017-08-03,31.288656981915 108 | 2017-08-04,31.526593536911 109 | 2017-08-07,31.56624962941 110 | 2017-08-08,31.397711236288 111 | 2017-08-09,31.348141120664 112 | 2017-08-10,30.733471686926 113 | 2017-08-11,31.199430773792 114 | 2017-08-14,31.56624962941 115 | 2017-08-15,31.81410020753 116 | 2017-08-16,32.061950785651 117 | 2017-08-17,30.773127779425 118 | 2017-08-18,30.108888230062 119 | 2017-08-21,30.416222946932 120 | 2017-08-22,31.001150311296 121 | 2017-08-23,30.654159501927 122 | 2017-08-24,30.971408241921 123 | 2017-08-25,31.169688704417 124 | 2017-08-28,31.268828935666 125 | 2017-08-29,31.209344796917 126 | 2017-08-30,31.714959976282 127 | 2017-08-31,31.933068485028 128 | 2017-09-01,32.042122739401 129 | 2017-09-05,31.348141120664 130 | 2017-09-06,31.595991698784 131 | 2017-09-07,31.486937444412 132 | -------------------------------------------------------------------------------- /09-Python-Finance-Fundamentals/GOOG_CLOSE: -------------------------------------------------------------------------------- 1 | Date,Adj. Close 2 | 2014-03-27,558.46 3 | 2014-03-28,559.99 4 | 2014-03-31,556.97 5 | 2014-04-01,567.16 6 | 2014-04-02,567.0 7 | 2014-04-03,569.74 8 | 2014-04-04,543.14 9 | 2014-04-07,538.15 10 | 2014-04-08,554.9 11 | 2014-04-09,564.14 12 | 2014-04-10,540.95 13 | 2014-04-11,530.6 14 | 2014-04-14,532.52 15 | 2014-04-15,536.44 16 | 2014-04-16,556.54 17 | 2014-04-17,536.1 18 | 2014-04-21,528.62 19 | 2014-04-22,534.81 20 | 2014-04-23,526.94 21 | 2014-04-24,525.16 22 | 2014-04-25,516.18 23 | 2014-04-28,517.15 24 | 2014-04-29,527.7 25 | 2014-04-30,526.66 26 | 2014-05-01,531.35 27 | 2014-05-02,527.93 28 | 2014-05-05,527.81 29 | 2014-05-06,515.14 30 | 2014-05-07,509.96 31 | 2014-05-08,511.0 32 | 2014-05-09,518.73 33 | 2014-05-12,529.92 34 | 2014-05-13,533.09 35 | 2014-05-14,526.65 36 | 2014-05-15,519.98 37 | 2014-05-16,520.63 38 | 2014-05-19,528.86 39 | 2014-05-20,529.77 40 | 2014-05-21,538.94 41 | 2014-05-22,545.06 42 | 2014-05-23,552.7 43 | 2014-05-27,565.95 44 | 2014-05-28,561.68 45 | 2014-05-29,560.08 46 | 2014-05-30,559.89 47 | 2014-06-02,553.93 48 | 2014-06-03,544.94 49 | 2014-06-04,544.66 50 | 2014-06-05,553.9 51 | 2014-06-06,556.33 52 | 2014-06-09,562.12 53 | 2014-06-10,560.55 54 | 2014-06-11,558.84 55 | 2014-06-12,551.35 56 | 2014-06-13,551.76 57 | 2014-06-16,544.28 58 | 2014-06-17,543.01 59 | 2014-06-18,553.37 60 | 2014-06-19,554.9 61 | 2014-06-20,556.36 62 | 2014-06-23,564.95 63 | 2014-06-24,564.62 64 | 2014-06-25,578.65 65 | 2014-06-26,576.0 66 | 2014-06-27,577.24 67 | 2014-06-30,575.28 68 | 2014-07-01,582.67 69 | 2014-07-02,582.335 70 | 2014-07-03,584.73 71 | 2014-07-07,582.25 72 | 2014-07-08,571.09 73 | 2014-07-09,576.08 74 | 2014-07-10,571.1 75 | 2014-07-11,579.18 76 | 2014-07-14,584.87 77 | 2014-07-15,584.78 78 | 2014-07-16,582.66 79 | 2014-07-17,573.7299 80 | 2014-07-18,595.08 81 | 2014-07-21,589.47 82 | 2014-07-22,594.74 83 | 2014-07-23,595.98 84 | 2014-07-24,593.35 85 | 2014-07-25,589.02 86 | 2014-07-28,590.6 87 | 2014-07-29,585.61 88 | 2014-07-30,587.42 89 | 2014-07-31,571.6 90 | 2014-08-01,566.07 91 | 2014-08-04,573.15 92 | 2014-08-05,565.07 93 | 2014-08-06,566.374 94 | 2014-08-07,563.36 95 | 2014-08-08,568.77 96 | 2014-08-11,567.88 97 | 2014-08-12,562.73 98 | 2014-08-13,574.78 99 | 2014-08-14,574.65 100 | 2014-08-15,573.48 101 | 2014-08-18,582.16 102 | 2014-08-19,586.86 103 | 2014-08-20,584.49 104 | 2014-08-21,583.37 105 | 2014-08-22,582.56 106 | 2014-08-25,580.2 107 | 2014-08-26,577.86 108 | 2014-08-27,571.0 109 | 2014-08-28,569.2 110 | 2014-08-29,571.6 111 | 2014-09-02,577.33 112 | 2014-09-03,577.94 113 | 2014-09-04,581.98 114 | 2014-09-05,586.08 115 | 2014-09-08,589.72 116 | 2014-09-09,581.01 117 | 2014-09-10,583.1 118 | 2014-09-11,581.35 119 | 2014-09-12,575.62 120 | 2014-09-15,573.1 121 | 2014-09-16,579.95 122 | 2014-09-17,584.77 123 | 2014-09-18,589.27 124 | 2014-09-19,596.08 125 | 2014-09-22,587.37 126 | 2014-09-23,581.13 127 | 2014-09-24,587.99 128 | 2014-09-25,575.06 129 | 2014-09-26,577.1 130 | 2014-09-29,576.36 131 | 2014-09-30,577.36 132 | 2014-10-01,568.27 133 | 2014-10-02,570.08 134 | 2014-10-03,575.28 135 | 2014-10-06,577.35 136 | 2014-10-07,563.74 137 | 2014-10-08,572.5 138 | 2014-10-09,560.88 139 | 2014-10-10,544.49 140 | 2014-10-13,533.21 141 | 2014-10-14,537.94 142 | 2014-10-15,530.03 143 | 2014-10-16,524.51 144 | 2014-10-17,511.17 145 | 2014-10-20,520.84 146 | 2014-10-21,526.54 147 | 2014-10-22,532.71 148 | 2014-10-23,543.98 149 | 2014-10-24,539.78 150 | 2014-10-27,540.77 151 | 2014-10-28,548.9 152 | 2014-10-29,549.33 153 | 2014-10-30,550.31 154 | 2014-10-31,559.08 155 | 2014-11-03,555.22 156 | 2014-11-04,554.11 157 | 2014-11-05,545.92 158 | 2014-11-06,542.04 159 | 2014-11-07,541.01 160 | 2014-11-10,547.49 161 | 2014-11-11,550.29 162 | 2014-11-12,547.31 163 | 2014-11-13,545.38 164 | 2014-11-14,544.4 165 | 2014-11-17,536.51 166 | 2014-11-18,535.03 167 | 2014-11-19,536.99 168 | 2014-11-20,534.83 169 | 2014-11-21,537.5 170 | 2014-11-24,539.27 171 | 2014-11-25,541.08 172 | 2014-11-26,540.37 173 | 2014-11-28,541.83 174 | 2014-12-01,533.8 175 | 2014-12-02,533.75 176 | 2014-12-03,531.32 177 | 2014-12-04,537.31 178 | 2014-12-05,525.26 179 | 2014-12-08,526.98 180 | 2014-12-09,533.37 181 | 2014-12-10,526.06 182 | 2014-12-11,528.34 183 | 2014-12-12,518.66 184 | 2014-12-15,513.8 185 | 2014-12-16,495.39 186 | 2014-12-17,504.89 187 | 2014-12-18,511.1 188 | 2014-12-19,516.35 189 | 2014-12-22,524.87 190 | 2014-12-23,530.59 191 | 2014-12-24,528.77 192 | 2014-12-26,534.03 193 | 2014-12-29,530.33 194 | 2014-12-30,530.42 195 | 2014-12-31,526.4 196 | 2015-01-02,524.81 197 | 2015-01-05,513.87 198 | 2015-01-06,501.96 199 | 2015-01-07,501.1 200 | 2015-01-08,502.68 201 | 2015-01-09,496.17 202 | 2015-01-12,492.55 203 | 2015-01-13,496.18 204 | 2015-01-14,500.87 205 | 2015-01-15,501.79 206 | 2015-01-16,508.08 207 | 2015-01-20,506.9 208 | 2015-01-21,518.04 209 | 2015-01-22,534.39 210 | 2015-01-23,539.95 211 | 2015-01-26,535.21 212 | 2015-01-27,518.63 213 | 2015-01-28,510.0 214 | 2015-01-29,510.66 215 | 2015-01-30,534.52 216 | 2015-02-02,528.48 217 | 2015-02-03,529.24 218 | 2015-02-04,522.76 219 | 2015-02-05,527.58 220 | 2015-02-06,531.0 221 | 2015-02-09,527.83 222 | 2015-02-10,536.94 223 | 2015-02-11,535.97 224 | 2015-02-12,542.93 225 | 2015-02-13,549.01 226 | 2015-02-17,542.84 227 | 2015-02-18,539.7 228 | 2015-02-19,542.87 229 | 2015-02-20,538.95 230 | 2015-02-23,531.91 231 | 2015-02-24,536.09 232 | 2015-02-25,543.87 233 | 2015-02-26,555.48 234 | 2015-02-27,558.4 235 | 2015-03-02,571.34 236 | 2015-03-03,573.64 237 | 2015-03-04,573.37 238 | 2015-03-05,575.33 239 | 2015-03-06,567.685 240 | 2015-03-09,568.85 241 | 2015-03-10,555.01 242 | 2015-03-11,551.18 243 | 2015-03-12,555.51 244 | 2015-03-13,547.32 245 | 2015-03-16,554.51 246 | 2015-03-17,550.84 247 | 2015-03-18,559.5 248 | 2015-03-19,557.99 249 | 2015-03-20,560.36 250 | 2015-03-23,558.81 251 | 2015-03-24,570.19 252 | 2015-03-25,558.785 253 | 2015-03-26,555.17 254 | 2015-03-27,548.34 255 | 2015-03-30,552.03 256 | 2015-03-31,548.0 257 | 2015-04-01,542.56 258 | 2015-04-02,535.53 259 | 2015-04-06,536.765 260 | 2015-04-07,537.02 261 | 2015-04-08,541.61 262 | 2015-04-09,540.78 263 | 2015-04-10,540.01 264 | 2015-04-13,539.17 265 | 2015-04-14,530.39 266 | 2015-04-15,532.53 267 | 2015-04-16,533.8 268 | 2015-04-17,524.05 269 | 2015-04-20,535.38 270 | 2015-04-21,533.97 271 | 2015-04-22,539.365 272 | 2015-04-23,547.0 273 | 2015-04-24,565.06 274 | 2015-04-27,555.37 275 | 2015-04-28,553.68 276 | 2015-04-29,549.08 277 | 2015-04-30,537.34 278 | 2015-05-01,537.9 279 | 2015-05-04,540.78 280 | 2015-05-05,530.8 281 | 2015-05-06,524.22 282 | 2015-05-07,530.7 283 | 2015-05-08,538.22 284 | 2015-05-11,535.7 285 | 2015-05-12,529.04 286 | 2015-05-13,529.62 287 | 2015-05-14,538.4 288 | 2015-05-15,533.85 289 | 2015-05-18,532.3 290 | 2015-05-19,537.36 291 | 2015-05-20,539.27 292 | 2015-05-21,542.51 293 | 2015-05-22,540.11 294 | 2015-05-26,532.32 295 | 2015-05-27,539.79 296 | 2015-05-28,539.78 297 | 2015-05-29,532.11 298 | 2015-06-01,533.99 299 | 2015-06-02,539.18 300 | 2015-06-03,540.31 301 | 2015-06-04,536.7 302 | 2015-06-05,533.33 303 | 2015-06-08,526.83 304 | 2015-06-09,526.69 305 | 2015-06-10,536.69 306 | 2015-06-11,534.61 307 | 2015-06-12,532.33 308 | 2015-06-15,527.2 309 | 2015-06-16,528.15 310 | 2015-06-17,529.26 311 | 2015-06-18,536.73 312 | 2015-06-19,536.69 313 | 2015-06-22,538.19 314 | 2015-06-23,540.48 315 | 2015-06-24,537.84 316 | 2015-06-25,535.23 317 | 2015-06-26,531.69 318 | 2015-06-29,521.52 319 | 2015-06-30,520.51 320 | 2015-07-01,521.84 321 | 2015-07-02,523.4 322 | 2015-07-06,522.86 323 | 2015-07-07,525.02 324 | 2015-07-08,516.83 325 | 2015-07-09,520.68 326 | 2015-07-10,530.13 327 | 2015-07-13,546.55 328 | 2015-07-14,561.1 329 | 2015-07-15,560.22 330 | 2015-07-16,579.85 331 | 2015-07-17,672.93 332 | 2015-07-20,663.02 333 | 2015-07-21,662.3 334 | 2015-07-22,662.1 335 | 2015-07-23,644.28 336 | 2015-07-24,623.56 337 | 2015-07-27,627.26 338 | 2015-07-28,628.0 339 | 2015-07-29,631.93 340 | 2015-07-30,632.59 341 | 2015-07-31,625.61 342 | 2015-08-03,631.21 343 | 2015-08-04,629.25 344 | 2015-08-05,643.78 345 | 2015-08-06,642.68 346 | 2015-08-07,635.3 347 | 2015-08-10,633.73 348 | 2015-08-11,660.78 349 | 2015-08-12,659.56 350 | 2015-08-13,656.45 351 | 2015-08-14,657.12 352 | 2015-08-17,660.87 353 | 2015-08-18,656.13 354 | 2015-08-19,660.9 355 | 2015-08-20,646.83 356 | 2015-08-21,612.48 357 | 2015-08-24,589.61 358 | 2015-08-25,582.06 359 | 2015-08-26,628.62 360 | 2015-08-27,637.61 361 | 2015-08-28,630.38 362 | 2015-08-31,618.25 363 | 2015-09-01,597.79 364 | 2015-09-02,614.34 365 | 2015-09-03,606.25 366 | 2015-09-04,600.7 367 | 2015-09-08,614.66 368 | 2015-09-09,612.72 369 | 2015-09-10,621.35 370 | 2015-09-11,625.77 371 | 2015-09-14,623.24 372 | 2015-09-15,635.14 373 | 2015-09-16,635.98 374 | 2015-09-17,642.9 375 | 2015-09-18,629.25 376 | 2015-09-21,635.44 377 | 2015-09-22,622.69 378 | 2015-09-23,622.36 379 | 2015-09-24,625.8 380 | 2015-09-25,611.97 381 | 2015-09-28,594.89 382 | 2015-09-29,594.97 383 | 2015-09-30,608.42 384 | 2015-10-01,611.29 385 | 2015-10-02,626.91 386 | 2015-10-05,641.47 387 | 2015-10-06,645.44 388 | 2015-10-07,642.36 389 | 2015-10-08,639.16 390 | 2015-10-09,643.61 391 | 2015-10-12,646.67 392 | 2015-10-13,652.3 393 | 2015-10-14,651.16 394 | 2015-10-15,661.74 395 | 2015-10-16,662.2 396 | 2015-10-19,666.1 397 | 2015-10-20,650.28 398 | 2015-10-21,642.61 399 | 2015-10-22,651.79 400 | 2015-10-23,702.0 401 | 2015-10-26,712.78 402 | 2015-10-27,708.49 403 | 2015-10-28,712.95 404 | 2015-10-29,716.92 405 | 2015-10-30,710.81 406 | 2015-11-02,721.11 407 | 2015-11-03,722.16 408 | 2015-11-04,728.11 409 | 2015-11-05,731.25 410 | 2015-11-06,733.76 411 | 2015-11-09,724.89 412 | 2015-11-10,728.32 413 | 2015-11-11,735.4 414 | 2015-11-12,731.23 415 | 2015-11-13,717.0 416 | 2015-11-16,728.96 417 | 2015-11-17,725.3 418 | 2015-11-18,740.0 419 | 2015-11-19,738.41 420 | 2015-11-20,756.6 421 | 2015-11-23,755.98 422 | 2015-11-24,748.28 423 | 2015-11-25,748.15 424 | 2015-11-27,750.26 425 | 2015-11-30,742.6 426 | 2015-12-01,767.04 427 | 2015-12-02,762.38 428 | 2015-12-03,752.54 429 | 2015-12-04,766.81 430 | 2015-12-07,763.25 431 | 2015-12-08,762.37 432 | 2015-12-09,751.61 433 | 2015-12-10,749.46 434 | 2015-12-11,738.87 435 | 2015-12-14,747.77 436 | 2015-12-15,743.4 437 | 2015-12-16,758.09 438 | 2015-12-17,749.43 439 | 2015-12-18,739.31 440 | 2015-12-21,747.77 441 | 2015-12-22,750.0 442 | 2015-12-23,750.31 443 | 2015-12-24,748.4 444 | 2015-12-28,762.51 445 | 2015-12-29,776.6 446 | 2015-12-30,771.0 447 | 2015-12-31,758.88 448 | 2016-01-04,741.84 449 | 2016-01-05,742.58 450 | 2016-01-06,743.62 451 | 2016-01-07,726.39 452 | 2016-01-08,714.47 453 | 2016-01-11,716.03 454 | 2016-01-12,726.07 455 | 2016-01-13,700.56 456 | 2016-01-14,714.72 457 | 2016-01-15,694.45 458 | 2016-01-19,701.79 459 | 2016-01-20,698.45 460 | 2016-01-21,706.59 461 | 2016-01-22,725.25 462 | 2016-01-25,711.67 463 | 2016-01-26,713.04 464 | 2016-01-27,699.99 465 | 2016-01-28,730.96 466 | 2016-01-29,742.95 467 | 2016-02-01,752.0 468 | 2016-02-02,764.65 469 | 2016-02-03,726.95 470 | 2016-02-04,708.01 471 | 2016-02-05,683.57 472 | 2016-02-08,682.74 473 | 2016-02-09,678.11 474 | 2016-02-10,684.12 475 | 2016-02-11,683.11 476 | 2016-02-12,682.4 477 | 2016-02-16,691.0 478 | 2016-02-17,708.4 479 | 2016-02-18,697.35 480 | 2016-02-19,700.91 481 | 2016-02-22,706.46 482 | 2016-02-23,695.85 483 | 2016-02-24,699.56 484 | 2016-02-25,705.75 485 | 2016-02-26,705.07 486 | 2016-02-29,697.77 487 | 2016-03-01,718.81 488 | 2016-03-02,718.85 489 | 2016-03-03,712.42 490 | 2016-03-04,710.89 491 | 2016-03-07,695.16 492 | 2016-03-08,693.97 493 | 2016-03-09,705.24 494 | 2016-03-10,712.82 495 | 2016-03-11,726.82 496 | 2016-03-14,730.49 497 | 2016-03-15,728.33 498 | 2016-03-16,736.09 499 | 2016-03-17,737.78 500 | 2016-03-18,737.6 501 | 2016-03-21,742.09 502 | 2016-03-22,740.75 503 | 2016-03-23,738.06 504 | 2016-03-24,735.3 505 | 2016-03-28,733.53 506 | 2016-03-29,744.77 507 | 2016-03-30,750.53 508 | 2016-03-31,744.95 509 | 2016-04-01,749.91 510 | 2016-04-04,745.29 511 | 2016-04-05,737.8 512 | 2016-04-06,745.69 513 | 2016-04-07,740.28 514 | 2016-04-08,739.15 515 | 2016-04-11,736.1 516 | 2016-04-12,743.09 517 | 2016-04-13,751.72 518 | 2016-04-14,753.2 519 | 2016-04-15,759.0 520 | 2016-04-18,766.61 521 | 2016-04-19,753.93 522 | 2016-04-20,752.67 523 | 2016-04-21,759.14 524 | 2016-04-22,718.77 525 | 2016-04-25,723.15 526 | 2016-04-26,708.14 527 | 2016-04-27,705.84 528 | 2016-04-28,691.02 529 | 2016-04-29,693.01 530 | 2016-05-02,698.21 531 | 2016-05-03,692.36 532 | 2016-05-04,695.7 533 | 2016-05-05,701.43 534 | 2016-05-06,711.12 535 | 2016-05-09,712.9 536 | 2016-05-10,723.18 537 | 2016-05-11,715.29 538 | 2016-05-12,713.31 539 | 2016-05-13,710.83 540 | 2016-05-16,716.49 541 | 2016-05-17,706.23 542 | 2016-05-18,706.63 543 | 2016-05-19,700.32 544 | 2016-05-20,709.74 545 | 2016-05-23,704.24 546 | 2016-05-24,720.09 547 | 2016-05-25,725.27 548 | 2016-05-26,724.12 549 | 2016-05-27,732.66 550 | 2016-05-31,735.72 551 | 2016-06-01,734.15 552 | 2016-06-02,730.4 553 | 2016-06-03,722.34 554 | 2016-06-06,716.55 555 | 2016-06-07,716.65 556 | 2016-06-08,728.28 557 | 2016-06-09,728.58 558 | 2016-06-10,719.41 559 | 2016-06-13,718.36 560 | 2016-06-14,718.27 561 | 2016-06-15,718.92 562 | 2016-06-16,710.36 563 | 2016-06-17,691.72 564 | 2016-06-20,693.71 565 | 2016-06-21,695.94 566 | 2016-06-22,697.46 567 | 2016-06-23,701.87 568 | 2016-06-24,675.22 569 | 2016-06-27,668.26 570 | 2016-06-28,680.04 571 | 2016-06-29,684.11 572 | 2016-06-30,692.1 573 | 2016-07-01,699.21 574 | 2016-07-05,694.49 575 | 2016-07-06,697.77 576 | 2016-07-07,695.36 577 | 2016-07-08,705.63 578 | 2016-07-11,715.09 579 | 2016-07-12,720.64 580 | 2016-07-13,716.98 581 | 2016-07-14,720.95 582 | 2016-07-15,719.85 583 | 2016-07-18,733.78 584 | 2016-07-19,736.96 585 | 2016-07-20,741.19 586 | 2016-07-21,738.63 587 | 2016-07-22,742.74 588 | 2016-07-25,739.77 589 | 2016-07-26,738.42 590 | 2016-07-27,741.77 591 | 2016-07-28,745.91 592 | 2016-07-29,768.79 593 | 2016-08-01,772.88 594 | 2016-08-02,771.07 595 | 2016-08-03,773.18 596 | 2016-08-04,771.61 597 | 2016-08-05,782.22 598 | 2016-08-08,781.76 599 | 2016-08-09,784.26 600 | 2016-08-10,784.68 601 | 2016-08-11,784.85 602 | 2016-08-12,783.22 603 | 2016-08-15,782.44 604 | 2016-08-16,777.14 605 | 2016-08-17,779.91 606 | 2016-08-18,777.5 607 | 2016-08-19,775.42 608 | 2016-08-22,772.15 609 | 2016-08-23,772.08 610 | 2016-08-24,769.64 611 | 2016-08-25,769.41 612 | 2016-08-26,769.54 613 | 2016-08-29,772.15 614 | 2016-08-30,769.09 615 | 2016-08-31,767.05 616 | 2016-09-01,768.78 617 | 2016-09-02,771.46 618 | 2016-09-06,780.08 619 | 2016-09-07,780.35 620 | 2016-09-08,775.32 621 | 2016-09-09,759.66 622 | 2016-09-12,769.02 623 | 2016-09-13,759.69 624 | 2016-09-14,762.49 625 | 2016-09-15,771.76 626 | 2016-09-16,768.88 627 | 2016-09-19,765.7 628 | 2016-09-20,771.41 629 | 2016-09-21,776.22 630 | 2016-09-22,787.21 631 | 2016-09-23,786.9 632 | 2016-09-26,774.21 633 | 2016-09-27,783.01 634 | 2016-09-28,781.56 635 | 2016-09-29,775.01 636 | 2016-09-30,777.29 637 | 2016-10-03,772.56 638 | 2016-10-04,776.43 639 | 2016-10-05,776.47 640 | 2016-10-06,776.86 641 | 2016-10-07,775.08 642 | 2016-10-10,785.94 643 | 2016-10-11,783.07 644 | 2016-10-12,786.14 645 | 2016-10-13,778.19 646 | 2016-10-14,778.53 647 | 2016-10-17,779.96 648 | 2016-10-18,795.26 649 | 2016-10-19,801.56 650 | 2016-10-20,796.97 651 | 2016-10-21,799.37 652 | 2016-10-24,813.11 653 | 2016-10-25,807.67 654 | 2016-10-26,799.07 655 | 2016-10-27,795.35 656 | 2016-10-28,795.37 657 | 2016-10-31,784.54 658 | 2016-11-01,783.61 659 | 2016-11-02,768.7 660 | 2016-11-03,762.13 661 | 2016-11-04,762.02 662 | 2016-11-07,782.52 663 | 2016-11-08,790.51 664 | 2016-11-09,785.31 665 | 2016-11-10,762.56 666 | 2016-11-11,754.02 667 | 2016-11-14,736.08 668 | 2016-11-15,758.49 669 | 2016-11-16,764.48 670 | 2016-11-17,771.23 671 | 2016-11-18,760.54 672 | 2016-11-21,769.2 673 | 2016-11-22,768.27 674 | 2016-11-23,760.99 675 | 2016-11-25,761.68 676 | 2016-11-28,768.24 677 | 2016-11-29,770.84 678 | 2016-11-30,758.04 679 | 2016-12-01,747.92 680 | 2016-12-02,750.5 681 | 2016-12-05,762.52 682 | 2016-12-06,759.11 683 | 2016-12-07,771.19 684 | 2016-12-08,776.42 685 | 2016-12-09,789.29 686 | 2016-12-12,789.27 687 | 2016-12-13,796.1 688 | 2016-12-14,797.07 689 | 2016-12-15,797.85 690 | 2016-12-16,790.8 691 | 2016-12-19,794.2 692 | 2016-12-20,796.42 693 | 2016-12-21,794.56 694 | 2016-12-22,791.26 695 | 2016-12-23,789.91 696 | 2016-12-27,791.55 697 | 2016-12-28,785.05 698 | 2016-12-29,782.79 699 | 2016-12-30,771.82 700 | -------------------------------------------------------------------------------- /09-Python-Finance-Fundamentals/IBM_CLOSE: -------------------------------------------------------------------------------- 1 | Date,Adj. Close 2 | 2017-03-06,175.10395614332 3 | 2017-03-07,175.01663217782 4 | 2017-03-08,174.11428453438 5 | 2017-03-09,171.91178007133 6 | 2017-03-10,172.54245315546 7 | 2017-03-13,171.21318834737 8 | 2017-03-14,170.49519129775 9 | 2017-03-15,170.58251526324 10 | 2017-03-16,171.96999604833 11 | 2017-03-17,170.42727265792 12 | 2017-03-20,170.47578597208 13 | 2017-03-21,168.70990133651 14 | 2017-03-22,169.58314099146 15 | 2017-03-23,169.62195164279 16 | 2017-03-24,168.66138802235 17 | 2017-03-27,168.60317204535 18 | 2017-03-28,169.32116909498 19 | 2017-03-29,168.76811731351 20 | 2017-03-30,168.69049601085 21 | 2017-03-31,168.96217057017 22 | 2017-04-03,169.31146643215 23 | 2017-04-04,169.33087175781 24 | 2017-04-05,167.73963505323 25 | 2017-04-06,167.32242055142 26 | 2017-04-07,167.02163800361 27 | 2017-04-10,166.10958769733 28 | 2017-04-11,165.50802260169 29 | 2017-04-12,165.58564390436 30 | 2017-04-13,164.48924300425 31 | 2017-04-17,166.012561069 32 | 2017-04-18,164.99378147156 33 | 2017-04-19,156.88235534334 34 | 2017-04-20,157.47421777614 35 | 2017-04-21,155.61130651225 36 | 2017-04-24,155.97030503706 37 | 2017-04-25,155.62100917508 38 | 2017-04-26,155.3008213016 39 | 2017-04-27,155.55309053525 40 | 2017-04-28,155.52398254675 41 | 2017-05-01,154.117096436 42 | 2017-05-02,154.36936566965 43 | 2017-05-03,153.91334051651 44 | 2017-05-04,154.32085235549 45 | 2017-05-05,150.43978722237 46 | 2017-05-08,149.93524875507 47 | 2017-05-09,149.0338540687 48 | 2017-05-10,148.19124599232 49 | 2017-05-11,147.60337989251 50 | 2017-05-12,147.32904237927 51 | 2017-05-15,148.4459879689 52 | 2017-05-16,150.57210369652 53 | 2017-05-17,147.87771740575 54 | 2017-05-18,147.7307508808 55 | 2017-05-19,148.90648308041 56 | 2017-05-22,149.5531357902 57 | 2017-05-23,148.95547192206 58 | 2017-05-24,149.42576480191 59 | 2017-05-25,150.10181081668 60 | 2017-05-26,149.40616926524 61 | 2017-05-30,148.66153887216 62 | 2017-05-31,149.54333802187 63 | 2017-06-01,149.58252909519 64 | 2017-06-02,148.97506745872 65 | 2017-06-05,149.3277871186 66 | 2017-06-06,149.28859604528 67 | 2017-06-07,147.9267062474 68 | 2017-06-08,149.02405630037 69 | 2017-06-09,150.98360996639 70 | 2017-06-12,152.04176894603 71 | 2017-06-13,151.13057649134 72 | 2017-06-14,150.69947468481 73 | 2017-06-15,151.10118318635 74 | 2017-06-16,152.23772431264 75 | 2017-06-19,151.70864482281 76 | 2017-06-20,151.81642027444 77 | 2017-06-21,150.67987914815 78 | 2017-06-22,151.27754301629 79 | 2017-06-23,150.99340773472 80 | 2017-06-26,152.09075778768 81 | 2017-06-27,151.62046490784 82 | 2017-06-28,152.17893770265 83 | 2017-06-29,151.01300327138 84 | 2017-06-30,150.71907022147 85 | 2017-07-03,152.43367967924 86 | 2017-07-05,150.56230592819 87 | 2017-07-06,149.27879827695 88 | 2017-07-07,149.8470688401 89 | 2017-07-10,150.31736171994 90 | 2017-07-11,150.09201304835 91 | 2017-07-12,150.59169923318 92 | 2017-07-13,150.52311485487 93 | 2017-07-14,151.12077872301 94 | 2017-07-17,149.91565321841 95 | 2017-07-18,150.88563228309 96 | 2017-07-19,144.54647617353 97 | 2017-07-20,144.67384716182 98 | 2017-07-21,144.10557659868 99 | 2017-07-24,143.0376198507 100 | 2017-07-25,143.2335752173 101 | 2017-07-26,142.4203604459 102 | 2017-07-27,142.13622516433 103 | 2017-07-28,141.37199923459 104 | 2017-07-31,141.74431443113 105 | 2017-08-01,142.36157383592 106 | 2017-08-02,141.52876352787 107 | 2017-08-03,142.00885417604 108 | 2017-08-04,142.2244050793 109 | 2017-08-07,140.56858223152 110 | 2017-08-08,140.70575098814 111 | 2017-08-09,140.36911067194 112 | 2017-08-10,140.43841897233 113 | 2017-08-11,140.43841897233 114 | 2017-08-14,140.91367588933 115 | 2017-08-15,140.66614624506 116 | 2017-08-16,141.0918972332 117 | 2017-08-17,139.30968379447 118 | 2017-08-18,138.31956521739 119 | 2017-08-21,138.94333992095 120 | 2017-08-22,139.61662055336 121 | 2017-08-23,140.73545454545 122 | 2017-08-24,141.52754940711 123 | 2017-08-25,142.31964426877 124 | 2017-08-28,141.10179841897 125 | 2017-08-29,141.72557312253 126 | 2017-08-30,141.15130434783 127 | 2017-08-31,141.61666007905 128 | 2017-09-01,142.65628458498 129 | 2017-09-05,141.62656126482 130 | 2017-09-06,142.39885375494 131 | 2017-09-07,141.48794466403 132 | -------------------------------------------------------------------------------- /10-Quantopian-Platform/01-Quantopian-Research-Basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 01-Quantopian Research Basics\n", 8 | "\n", 9 | "**Please remember that this notebook will only work on Quantopian! Make an account and upload this notebook file. These commands and functions won't work except on the Quantopian trading platform!**\n", 10 | "\n", 11 | "Note a lot of the written markdown text in this notebook comes direclty from the Quantopian docs and tutorials, definitely check those out as well, they're great!\n", 12 | "\n", 13 | "## Research\n", 14 | "\n", 15 | "The notebook format allows us to easily gather information about variuos securities all within the Quantopian platform. Keep in mind this is different than the base coding platform of quantopian, which focuses on actually implementing and backtesting trading strategies." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 2, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "import numpy as np\n", 25 | "import pandas as pd\n", 26 | "import matplotlib.pyplot as plt\n", 27 | "# NO NEED TO DO MAGIC INLINE COMMAND ON QUANTOPIAN!" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "source": [ 36 | "## Getting Information\n", 37 | "\n", 38 | "Let's go over a few key functions:\n", 39 | "\n", 40 | "* get_pricing()\n", 41 | "* symbols()\n", 42 | "* local_csv()\n", 43 | "* get_backtest()\n", 44 | "* get_fundamentals()" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "## get_pricing()\n", 52 | "\n", 53 | "The `get_pricing` function provides access to 12 years of US Equity pricing data: the same data used by the Quantopian backtester.\n", 54 | "\n", 55 | "`get_pricing` returns a pandas object. This could be a panel, dataframe or series depending on the input values. " 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "ename": "NameError", 65 | "evalue": "name 'get_pricing' is not defined", 66 | "output_type": "error", 67 | "traceback": [ 68 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 69 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 70 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m mcdon = get_pricing('MCD',\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mstart_date\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'2017-01-01'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mend_date\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'2017-02-01'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m frequency='minute')\n", 71 | "\u001b[0;31mNameError\u001b[0m: name 'get_pricing' is not defined" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "mcdon = get_pricing('MCD',\n", 77 | " start_date='2017-01-01', \n", 78 | " end_date = '2017-02-01', \n", 79 | " frequency='minute')" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "mcdon.head()" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "mcdon.info()" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "# Can only go about 12 years back\n", 107 | "# which is really all you need for algo trading, \n", 108 | "# going back further probably is more noise than signal.\n", 109 | "\n", 110 | "mcdon = get_pricing('MCD',\n", 111 | " start_date='2005-01-01', \n", 112 | " end_date = '2017-01-01', \n", 113 | " frequency='daily')" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "mcdon['close_price'].plot()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "mcdon['close_price'].pct_change(1).hist(bins=100,figsize=(6,4))" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "## symbols()\n", 139 | "\n", 140 | "By default `symbols` returns the security object for a ticker symbol. Specify a ticker symbol, or list of symbols, as a string and get a list of security objects back. \n", 141 | "\n", 142 | "- Use `symbol_reference_date` to identify which date you want the symbol back for a particular ticker symbol. \n", 143 | "- Specify how you would like missing results to be handled with `handle_missing`\n", 144 | "\n" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "mcdon_eq_info = symbols('MCD')" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [ 162 | "type(mcdon_eq_info)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [ 171 | "mcdon_eq_info.to_dict()" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "for key in mcdon_eq_info.to_dict():\n", 181 | " print(key)\n", 182 | " print(mcdon_eq_info.to_dict()[key])\n", 183 | " print('\\n')" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "## get_fundamentals()\n", 191 | "\n", 192 | "The `get_fundamentals` function provides programmatic access to the Quantopian fundamental database. Based on data provided by Morningstar, `get_fundamentals` provides over 600 corporate metrics dating back to 2002 (to match Quantopian's pricing data). \n", 193 | "\n", 194 | "The data used by this research function is the same data used by the `get_fundamentals` function used in the Quantopian IDE. The fields are described in the Quantopian help documents: http://www.quantopian.com/help/fundamentals.\n" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": null, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "# Have to do this first in the notebook:\n", 204 | "fundamentals = init_fundamentals()" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "The get_fundamentals() function takes in a SQLAlchemy query which can be quite complicated and strange looking at first. Basically it allows you to filter by a variety of fundamentals (things like Market Cap, P/E Ratio, or even city of HQ). Check out the link above for all the things you can filter by!\n", 212 | "\n", 213 | "Let's walk through a few query examples.\n", 214 | "\n", 215 | "First call fundamentals and use tab to check out the various options:" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [ 224 | "fundamentals. # call tab here as in the video!" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "# Market Cap\n", 234 | "my_query = query(fundamentals.valuation.market_cap)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": {}, 241 | "outputs": [], 242 | "source": [ 243 | "my_funds = get_fundamentals(my_query,'2017-01-01')" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [ 252 | "my_funds.info()" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "# Basically just returns the market cap of everything\n", 262 | "# for 2017-01-01\n", 263 | "my_funds.head()" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "# What you usualy do is filter by other qualities after the query!\n", 273 | "\n", 274 | "# Only get companies worth 500 billion or more (that's a lot of dough!)\n", 275 | "big_companies = (query(fundamentals.valuation.market_cap).\n", 276 | " filter(fundamentals.valuation.market_cap > 500000000000) )" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "metadata": {}, 283 | "outputs": [], 284 | "source": [ 285 | "my_big_funds = get_fundamentals(big_companies,'2017-07-19')" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": null, 291 | "metadata": {}, 292 | "outputs": [], 293 | "source": [ 294 | "# On \n", 295 | "my_big_funds" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "metadata": {}, 302 | "outputs": [], 303 | "source": [ 304 | "7.82 * 10**11" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": {}, 311 | "outputs": [], 312 | "source": [ 313 | "get_fundamentals()" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [] 322 | } 323 | ], 324 | "metadata": { 325 | "kernelspec": { 326 | "display_name": "Python 2", 327 | "language": "python", 328 | "name": "python2" 329 | }, 330 | "language_info": { 331 | "codemirror_mode": { 332 | "name": "ipython", 333 | "version": 2 334 | }, 335 | "file_extension": ".py", 336 | "mimetype": "text/x-python", 337 | "name": "python", 338 | "nbconvert_exporter": "python", 339 | "pygments_lexer": "ipython2", 340 | "version": "2.7.14" 341 | } 342 | }, 343 | "nbformat": 4, 344 | "nbformat_minor": 2 345 | } 346 | -------------------------------------------------------------------------------- /10-Quantopian-Platform/02-Basic-Algorithm-Methods.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Basic Algorithm Methods\n", 8 | "\n", 9 | "Let's algorithmically test our earlier optimized tech portfolio strategy with Quantopian!\n", 10 | "\n", 11 | "#### THIS CODE ONLY WORKS ON QUANTOPIAN. EACH CELL CORRESPONDS WITH A PART OF THE VIDEO LECTURE. MAKE SURE TO WATCH THE VIDEOS FOR CLARITY ON THIS!" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "**initialize()**\n", 19 | "\n", 20 | "initialize() is called exactly once when our algorithm starts and requires context as input.\n", 21 | "\n", 22 | "context is an augmented Python dictionary used for maintaining state during our backtest or live trading, and can be referenced in different parts of our algorithm. context should be used instead of global variables in the algorithm. Properties can be accessed using dot notation (context.some_property)." 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "** handle_data() **\n", 30 | "\n", 31 | "handle_data() is called once at the end of each minute and requires context and data as input. context is a reference to the same dictionary in initialize() and data is an object that stores several API functions." 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "## Our Tech Stock Optimized Portfolio\n", 39 | "\n", 40 | "Let's use the tech stock portfolio we calculated earlier. Keep in mind that handle_data() is readjusting our portfolio every minute! That may be unreasonable for certain algorithms, but for this example, we will just continue with these basics functions." 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 1, 46 | "metadata": { 47 | "collapsed": true 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "def initialize(context):\n", 52 | " # Reference to Tech Stocks\n", 53 | " context.aapl = sid(24)\n", 54 | " context.csco = sid(1900)\n", 55 | " context.amzn = sid(16841)\n", 56 | "\n", 57 | "def handle_data(context, data):\n", 58 | " # Position our portfolio optimization!\n", 59 | " order_target_percent(context.aapl, .27)\n", 60 | " order_target_percent(context.csco, .20)\n", 61 | " order_target_percent(context.amzn, .53)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "### Grabbing Current Data" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "### data.current()\n", 76 | "data.current() can be used to retrieve the most recent value of a given field(s) for a given asset(s). data.current() requires two arguments: the asset or list of assets, and the field or list of fields being queried. Possible fields include 'price', 'open', 'high', 'low', 'close', and 'volume'. The output type will depend on the input types" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 3, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "def initialize(context):\n", 86 | " # Reference to Tech Stocks\n", 87 | " context.techies = [sid(16841),sid(24),sid(1900)]\n", 88 | "\n", 89 | "def handle_data(context, data):\n", 90 | " # Position our portfolio optimization!\n", 91 | " tech_close = data.current(context.techies,'close')\n", 92 | " print(type(tech_close)) # Pandas Series\n", 93 | " print(tech_close) # Closing Prices " 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "##### Note! You can use data.is_stale(sid(#)) to check if the results of data.current() where generated at the current bar (the timeframe) or were forward filled from a previous time." 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "### Checking for trading" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "### data.can_trade()\n", 115 | "\n", 116 | "data.can_trade() is used to determine if an asset(s) is currently listed on a supported exchange and can be ordered. If data.can_trade() returns True for a particular asset in a given minute bar, we are able to place an order for that asset in that minute. This is an important guard to have in our algorithm if we hand-pick the securities that we want to trade. It requires a single argument: an asset or a list of assets." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 5, 122 | "metadata": { 123 | "collapsed": true 124 | }, 125 | "outputs": [], 126 | "source": [ 127 | "def initialize(context):\n", 128 | " # Reference to amazn\n", 129 | " context.amzn = sid(16841)\n", 130 | " \n", 131 | "def handle_data(context, data):\n", 132 | " # This insures we don't hit an exception!\n", 133 | " if data.can_trade(sid(16841)):\n", 134 | " order_target_percent(context.amzn, 1.0)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "# Checking Historical Data\n", 142 | "\n", 143 | "When your algorithm calls data.history on equities, the returned data is adjusted for splits, mergers, and dividends as of the current simulation date. In other words, when your algorithm asks for a historical window of prices, and there is a split in the middle of that window, the first part of that window will be adjusted for the split. This adustment is done so that your algorithm can do meaningful calculations using the values in the window.\n", 144 | "\n", 145 | "This code queries the last 20 days of price history for a static set of securities. Specifically, this returns the closing daily price for the last 20 days, including the current price for the current day. Equity prices are split- and dividend-adjusted as of the current date in the simulation:" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 8, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "\n", 155 | "def initialize(context):\n", 156 | " # AAPL, MSFT, and SPY\n", 157 | " context.assets = [sid(24), sid(1900), sid(16841)]\n", 158 | "\n", 159 | "def before_trading_start(context,data):\n", 160 | " price_history = data.history(context.assets,fields=\"price\", bar_count=5, frequency=\"1d\")\n", 161 | " \n", 162 | " print(price_history)\n" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "The bar_count field specifies the number of days or minutes to include in the pandas DataFrame returned by the history function. This parameter accepts only integer values.\n", 170 | "\n", 171 | "The frequency field specifies how often the data is sampled: daily or minutely. Acceptable inputs are ‘1d’ or ‘1m’. For other frequencies, use the pandas resample function." 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "### Examples\n", 179 | "Below are examples of code along with explanations of the data returned.\n", 180 | "\n", 181 | "### Daily History\n", 182 | "\n", 183 | "Use \"1d\" for the frequency. The dataframe returned is always in daily bars. The bars never span more than one trading day. For US equities, a daily bar captures the trade activity during market hours (usually 9:30am-4:00pm ET). For US futures, a daily bar captures the trade activity from 6pm-6pm ET (24 hours). For example, the Monday daily bar captures trade activity from 6pm the day before (Sunday) to 6pm on the Monday. Tuesday's daily bar will run from 6pm Monday to 6pm Tuesday, etc. For either asset class, the last bar, if partial, is built using the minutes of the current day.\n", 184 | "\n", 185 | "### Examples (assuming context.assets exists):\n", 186 | "\n", 187 | "* data.history(context.assets, \"price\", 1, \"1d\") returns the current price.\n", 188 | "* data.history(context.assets, \"volume\", 1, \"1d\") returns the volume since the current day's open, even if it is partial.\n", 189 | "* data.history(context.assets, \"price\", 2, \"1d\") returns yesterday's close price and the current price.\n", 190 | "* data.history(context.assets, \"price\", 6, \"1d\") returns the prices for the previous 5 days and the current price.\n", 191 | "\n", 192 | "\n", 193 | "### Minute History\n", 194 | "\n", 195 | "Use \"1m\" for the frequency.\n", 196 | "\n", 197 | "Examples (assuming context.assets exists):\n", 198 | "\n", 199 | "* data.history(context.assets, \"price\", 1, \"1m\") returns the current price.\n", 200 | "* data.history(context.assets, \"price\", 2, \"1m\") returns the previous minute's close price and the current price.\n", 201 | "* data.history(context.assets, \"volume\", 60, \"1m\") returns the volume for the previous 60 minutes." 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "# Scheduling\n", 209 | "\n", 210 | "Use schedule_function to indicate when you want other functions to occur. The functions passed in must take context and data as parameters." 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 2, 216 | "metadata": { 217 | "collapsed": true 218 | }, 219 | "outputs": [], 220 | "source": [ 221 | "def initialize(context):\n", 222 | " context.appl = sid(49051)\n", 223 | "\n", 224 | " # At ebginning of trading week\n", 225 | " # At Market Open, set 10% of portfolio to be apple\n", 226 | " schedule_function(open_positions, date_rules.week_start(), time_rules.market_open())\n", 227 | " \n", 228 | " # At end of trading week\n", 229 | " # 30 min before market close, dump all apple stock.\n", 230 | " schedule_function(close_positions, date_rules.week_end(), time_rules.market_close(minutes=30))\n", 231 | "\n", 232 | "def open_positions(context, data):\n", 233 | " order_target_percent(context.appl, 0.10)\n", 234 | "\n", 235 | "def close_positions(context, data):\n", 236 | " order_target_percent(context.appl, 0)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "# Portfolio Information\n", 244 | "\n", 245 | "You can get portfolio information and record it!" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 3, 251 | "metadata": { 252 | "collapsed": true 253 | }, 254 | "outputs": [], 255 | "source": [ 256 | "def initialize(context):\n", 257 | " context.amzn = sid(16841)\n", 258 | " context.ibm = sid(3766)\n", 259 | "\n", 260 | " schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())\n", 261 | " schedule_function(record_vars, date_rules.every_day(), time_rules.market_close())\n", 262 | "\n", 263 | "def rebalance(context, data):\n", 264 | " # Half of our portfolio long on amazn\n", 265 | " order_target_percent(context.amzn, 0.50)\n", 266 | " # Half is shorting IBM\n", 267 | " order_target_percent(context.ibm, -0.50)\n", 268 | "\n", 269 | "def record_vars(context, data):\n", 270 | "\n", 271 | " # Plot the counts\n", 272 | " record(amzn_close=data.current(context.amzn,'close'))\n", 273 | " record(ibm_close=data.current(context.ibm,'close'))" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "# Slippage and Commision \n", 281 | "\n", 282 | "### Slippage\n", 283 | "Slippage is where a simulation estimates the impact of orders on the fill rate and execution price they receive. When an order is placed for a trade, the market is affected. Buy orders drive prices up, and sell orders drive prices down; this is generally referred to as the price_impact of a trade. Additionally, trade orders do not necessarily fill instantaneously. Fill rates are dependent on the order size and current trading volume of the ordered security. The volume_limit determines the fraction of a security's trading volume that can be used by your algorithm.\n", 284 | "\n", 285 | "In backtesting and non-brokerage paper trading (Quantopian paper trading), a slippage model can be specified in initialize() using set_slippage(). There are different builtin slippage models that can be used, as well as the option to set a custom model. By default (if a slippage model is not specified), the following volume share slippage model is used:" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": null, 291 | "metadata": {}, 292 | "outputs": [], 293 | "source": [ 294 | "set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Using the default model, if an order of 60 shares is placed for a given stock, then 1000 shares of that stock trade in each of the next several minutes and the volume_limit is 0.025, then our trade order will be split into three orders (25 shares, 25 shares, and 10 shares) that execute over the next 3 minutes.\n", 302 | "\n", 303 | "At the end of each day, all open orders are canceled, so trading liquid stocks is generally a good idea. Additionally, orders placed exactly at market close will not have time to fill, and will be canceled." 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "### Commision\n", 311 | "\n", 312 | "To set the cost of trades, we can specify a commission model in initialize() using set_commission(). By default (if a commission model is not specified), the following commission model is used:" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": { 319 | "collapsed": true 320 | }, 321 | "outputs": [], 322 | "source": [ 323 | "set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1))" 324 | ] 325 | }, 326 | { 327 | "cell_type": "markdown", 328 | "metadata": {}, 329 | "source": [ 330 | "The default commission model charges $0.0075 per share, with a minimum trade cost of $1.\n", 331 | "\n", 332 | "Slippage and commission models can have an impact on the performance of a backtest. The default models used by Quantopian are fairly realistic, and it is highly recommended that you use them." 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": {}, 338 | "source": [ 339 | "# Great Job!\n", 340 | "\n", 341 | "Those are all the basics of Quantopians Tutorial! With these key functions you actually know enough to begin trading! " 342 | ] 343 | } 344 | ], 345 | "metadata": { 346 | "anaconda-cloud": {}, 347 | "kernelspec": { 348 | "display_name": "Python 2", 349 | "language": "python", 350 | "name": "python2" 351 | }, 352 | "language_info": { 353 | "codemirror_mode": { 354 | "name": "ipython", 355 | "version": 2 356 | }, 357 | "file_extension": ".py", 358 | "mimetype": "text/x-python", 359 | "name": "python", 360 | "nbconvert_exporter": "python", 361 | "pygments_lexer": "ipython2", 362 | "version": "2.7.14" 363 | } 364 | }, 365 | "nbformat": 4, 366 | "nbformat_minor": 1 367 | } 368 | -------------------------------------------------------------------------------- /10-Quantopian-Platform/04-Trading-Algorithm-Exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Trading Algorithm Exercise \n", 8 | "** This is an extremely open exercise and there are lots of ways to do it! Please feel free to just skip to the solutions to treat it as an example code along. If you attempt the exercise, do not expect the example solution to exactly match up to your solution. You may have performed much better (or much worse)! **\n", 9 | "\n", 10 | "## Your Task\n", 11 | "\n", 12 | "Your manager wants to see if [Bollinger Bands](https://en.wikipedia.org/wiki/Bollinger_Bands) are still a meaningful technical analysis strategy on their own. For this exercise, you will be testing *Johnson and Johnson* sid(4151).Specifically, your manager has decided he wants set 100% of the portfolio to go long when the stock price is below 2 times the 20 day rolling standard deviation subtracted from the 20 day moving average, and go 100% short of the portfolio on that stock when the current price is above 2 times the 20 day rolling standard deviation added on to the 20 day moving average. The check for this signal event should only happen once per day. This is probably a very unreasonable strategy, but the main point of this is to exercise your ability to write out backtest algorithms with Quantopian.\n", 13 | "\n", 14 | "## Time Frame\n", 15 | "\n", 16 | "You should use the following time frame for this exercise (so you can at least have a reasonable comparison to the solutions, but feel free to play around with this!)\n", 17 | "\n", 18 | "#### BACKTEST START: Jul-20-2014\n", 19 | "#### BACKTEST END: Jul-20-2017" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [] 30 | } 31 | ], 32 | "metadata": { 33 | "anaconda-cloud": {}, 34 | "kernelspec": { 35 | "display_name": "Python 3", 36 | "language": "python", 37 | "name": "python3" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 3 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython3", 49 | "version": "3.6.1" 50 | } 51 | }, 52 | "nbformat": 4, 53 | "nbformat_minor": 1 54 | } 55 | -------------------------------------------------------------------------------- /10-Quantopian-Platform/05-Trading-Algorithm-Exercise-Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Trading Algorithm Exercise Solutions\n", 8 | "\n", 9 | "** This is an extremely open exercise and there are lots of ways to do it! Please feel free to just skip to the solutions to treat it as an example code along. If you attempt the exercise, do not expect the example solution to exactly match up to your solution. You may have performed much better (or much worse)! **\n", 10 | "\n", 11 | "## Your Task\n", 12 | "\n", 13 | "Your manager wants to see if [Bollinger Bands](https://en.wikipedia.org/wiki/Bollinger_Bands) are still a meaningful technical analysis strategy on their own. For this exercise, you will be testing *Johnson and Johnson* sid(4151).Specifically, your manager has decided he wants set 100% of the portfolio to go long when the stock price is below 2 times the 20 day rolling standard deviation subtracted from the 20 day moving average, and go 100% short of the portfolio on that stock when the current price is above 2 times the 20 day rolling standard deviation added on to the 20 day moving average. The check for this signal event should only happen once per day. This is probably a very unreasonable strategy, but the main point of this is to exercise your ability to write out backtest algorithms with Quantopian.\n", 14 | "\n", 15 | "## Time Frame\n", 16 | "\n", 17 | "You should use the following time frame for this exercise (so you can at least have a reasonable comparison to the solutions, but feel free to play around with this!)\n", 18 | "\n", 19 | "#### BACKTEST START: Jul-20-2014\n", 20 | "#### BACKTEST END: Jul-20-2017" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": { 27 | "collapsed": true 28 | }, 29 | "outputs": [], 30 | "source": [ 31 | "# Setup our variables\n", 32 | "def initialize(context):\n", 33 | " context.jj = sid(4151)\n", 34 | "\n", 35 | " schedule_function(check_bands,date_rules.every_day())\n", 36 | " \n", 37 | "def check_bands(context, data):\n", 38 | " \n", 39 | " cur_price = data.current(context.jj,'price')\n", 40 | " \n", 41 | " # Load historical data for the stocks\n", 42 | " prices = data.history(context.jj,'price', 20 , '1d')\n", 43 | " \n", 44 | " avg = prices.mean()\n", 45 | " std = prices.std()\n", 46 | " lower_band = avg - 2*std\n", 47 | " upper_band = avg + 2*std\n", 48 | " \n", 49 | " if cur_price <= lower_band:\n", 50 | " order_target_percent(context.jj, 1.0)\n", 51 | " print('Buying')\n", 52 | " print('Current price is: ' + str(cur_price))\n", 53 | " print(\"Lower band is: \"+str(lower_band))\n", 54 | " \n", 55 | " \n", 56 | " elif cur_price >= upper_band:\n", 57 | " order_target_percent(context.jj, -1.0)\n", 58 | " print('Shorting')\n", 59 | " print('Current price is: ' + str(cur_price))\n", 60 | " print(\"Upper band is: \"+str(upper_band))\n", 61 | " else:\n", 62 | " pass\n", 63 | " \n", 64 | " record(upper=upper_band,\n", 65 | " lower=lower_band,\n", 66 | " mvag_20=avg,\n", 67 | " price=cur_price)" 68 | ] 69 | } 70 | ], 71 | "metadata": { 72 | "anaconda-cloud": {}, 73 | "kernelspec": { 74 | "display_name": "Python 3", 75 | "language": "python", 76 | "name": "python3" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 3 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython3", 88 | "version": "3.6.1" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 1 93 | } 94 | -------------------------------------------------------------------------------- /11-Advanced-Quantopian-Topics/04-Stock-Sentiment-Analysis-Project.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Stock Sentiment Analysis\n", 8 | "\n", 9 | "Check out the video for full details." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Final Code\n", 17 | "Here is the final code:" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": { 24 | "collapsed": true 25 | }, 26 | "outputs": [], 27 | "source": [ 28 | "# This section is only importable in the backtester\n", 29 | "from quantopian.algorithm import attach_pipeline, pipeline_output\n", 30 | "\n", 31 | "# General pipeline imports\n", 32 | "from quantopian.pipeline import Pipeline\n", 33 | "from quantopian.pipeline.factors import AverageDollarVolume\n", 34 | "\n", 35 | "\n", 36 | "# Using the free sample in your pipeline algo\n", 37 | "from quantopian.pipeline.data.accern import alphaone_free\n", 38 | "\n", 39 | "\n", 40 | "def initialize(context):\n", 41 | " # Schedule our rebalance function to run at the start of each week.\n", 42 | " schedule_function(my_rebalance, date_rules.every_day())\n", 43 | "\n", 44 | " \n", 45 | " attach_pipeline(make_pipeline(), \"pipeline\")\n", 46 | "\n", 47 | "def make_pipeline():\n", 48 | "\n", 49 | " \n", 50 | " # Screen out penny stocks and low liquidity securities.\n", 51 | " dollar_volume = AverageDollarVolume(window_length=20)\n", 52 | " is_liquid = dollar_volume.rank(ascending=False) < 1000\n", 53 | " \n", 54 | " # Add pipeline factors\n", 55 | " impact = alphaone_free.impact_score.latest\n", 56 | " sentiment = alphaone_free.article_sentiment.latest\n", 57 | "\n", 58 | " return Pipeline(columns={\n", 59 | " 'impact': impact,\n", 60 | " 'sentiment':sentiment,\n", 61 | " },\n", 62 | " screen = is_liquid)\n", 63 | " \n", 64 | "\n", 65 | "\n", 66 | " \n", 67 | "def before_trading_start(context, data):\n", 68 | " port = pipeline_output('pipeline')\n", 69 | " \n", 70 | " # Grab stocks with 100 impact and >0.5 sentiment and go long.\n", 71 | " context.longs = port[(port['impact']==100) & (port['sentiment']>0.75)].index.tolist()\n", 72 | " \n", 73 | " # Grab stocks with 100 impact and <-0.5 sentiment and go long.\n", 74 | " context.shorts = port[(port['impact']==100) & (port['sentiment']< -0.75)].index.tolist()\n", 75 | "\n", 76 | " context.long_weight, context.short_weight = my_compute_weights(context)\n", 77 | "\n", 78 | "def my_compute_weights(context):\n", 79 | "\n", 80 | " # Compute even target weights for our long positions and short positions.\n", 81 | " long_weight = 0.5 / len(context.longs)\n", 82 | " short_weight = -0.5 / len(context.shorts)\n", 83 | "\n", 84 | " return long_weight, short_weight\n", 85 | "\n", 86 | "def my_rebalance(context, data):\n", 87 | "\n", 88 | " for security in context.portfolio.positions:\n", 89 | " if security not in context.longs and security not in context.shorts and data.can_trade(security):\n", 90 | " order_target_percent(security, 0)\n", 91 | "\n", 92 | " for security in context.longs:\n", 93 | " if data.can_trade(security):\n", 94 | " order_target_percent(security, context.long_weight)\n", 95 | "\n", 96 | " for security in context.shorts:\n", 97 | " if data.can_trade(security):\n", 98 | " order_target_percent(security, context.short_weight)\n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | "\n", 103 | " \n" 104 | ] 105 | } 106 | ], 107 | "metadata": { 108 | "kernelspec": { 109 | "display_name": "Python 3", 110 | "language": "python", 111 | "name": "python3" 112 | }, 113 | "language_info": { 114 | "codemirror_mode": { 115 | "name": "ipython", 116 | "version": 3 117 | }, 118 | "file_extension": ".py", 119 | "mimetype": "text/x-python", 120 | "name": "python", 121 | "nbconvert_exporter": "python", 122 | "pygments_lexer": "ipython3", 123 | "version": "3.6.1" 124 | } 125 | }, 126 | "nbformat": 4, 127 | "nbformat_minor": 2 128 | } 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-for-Finance-Repo 2 | 3 | In this course, I learnt many useful package in python that frquently used in quantitative finance field, including: 4 | 5 | 1.NumPy for High Speed Numerical Processing 6 | 7 | 2.Pandas for Efficient Data Analysis 8 | 9 | 3.Matplotlib for Data Visualization 10 | 11 | 4.Using pandas-datareader and Quandl for data ingestion 12 | 13 | 14 | Also this course covers many classic financial analytic methods: 15 | 16 | 1.Pandas Time Series Analysis Techniques 17 | 18 | 2.Stock Returns Analysis 19 | 20 | 3.Cumulative Daily Returns 21 | 22 | 4.Volatility and Securities Risk 23 | 24 | 5.EWMA (Exponentially Weighted Moving Average) 25 | 26 | 6.Statsmodels 27 | 28 | 7.ETS (Error-Trend-Seasonality) 29 | 30 | 8.ARIMA (Auto-regressive Integrated Moving Averages) 31 | 32 | 9.Auto Correlation Plots and Partial Auto Correlation Plots 33 | 34 | 10.Sharpe Ratio 35 | 36 | 11.Portfolio Allocation Optimization 37 | 38 | 12.Efficient Frontier and Markowitz Optimization 39 | 40 | 13.Capital Asset Pricing Model 41 | 42 | 14.Stock Splits and Dividends 43 | 44 | 15.Efficient Market Hypothesis 45 | 46 | And finally, it teaches how to implement the above algorithms and fit them into Quantopian. 47 | 48 | --------------------------------------------------------------------------------