|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:683cfd7e6b5fd1dca9cd2028b294ec8ae44a175b613eb3c037d365a28957a987" |
| 4 | + "signature": "sha256:13c10e46fea51bcbbf203261897bb4d8ea2087e39925e077d452778f78d62b1e" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
56 | 56 | "## Sections\n", |
57 | 57 | "- [Reading in a CSV file](#reading)\n", |
58 | 58 | "- [Printing the CSV file contents](#printing)\n", |
| 59 | + "- [Converting numeric cells to floats](#floats)\n", |
59 | 60 | "- [Sorting the CSV file](#sorting)\n", |
60 | 61 | "- [Marking min/max values in particular columns](#marking)\n", |
61 | 62 | "- [Writing out the modified table to as a new CSV file](#writing)\n", |
|
140 | 141 | "language": "python", |
141 | 142 | "metadata": {}, |
142 | 143 | "outputs": [], |
143 | | - "prompt_number": 1 |
| 144 | + "prompt_number": 4 |
144 | 145 | }, |
145 | 146 | { |
146 | 147 | "cell_type": "code", |
|
166 | 167 | ] |
167 | 168 | } |
168 | 169 | ], |
169 | | - "prompt_number": 3 |
| 170 | + "prompt_number": 5 |
170 | 171 | }, |
171 | 172 | { |
172 | 173 | "cell_type": "markdown", |
|
212 | 213 | "language": "python", |
213 | 214 | "metadata": {}, |
214 | 215 | "outputs": [], |
215 | | - "prompt_number": 4 |
| 216 | + "prompt_number": 6 |
216 | 217 | }, |
217 | 218 | { |
218 | 219 | "cell_type": "code", |
|
245 | 246 | ] |
246 | 247 | } |
247 | 248 | ], |
248 | | - "prompt_number": 5 |
| 249 | + "prompt_number": 7 |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "markdown", |
| 253 | + "metadata": {}, |
| 254 | + "source": [ |
| 255 | + "<a name='floats'></a>\n", |
| 256 | + "<br>\n", |
| 257 | + "<br>" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "markdown", |
| 262 | + "metadata": {}, |
| 263 | + "source": [ |
| 264 | + "## Converting numeric cells to floats" |
| 265 | + ] |
| 266 | + }, |
| 267 | + { |
| 268 | + "cell_type": "markdown", |
| 269 | + "metadata": {}, |
| 270 | + "source": [ |
| 271 | + "To avoid problems with the sorting approach that can occur when we have negative values in some cells, let us define a function that converts all numeric cells into float values." |
| 272 | + ] |
| 273 | + }, |
| 274 | + { |
| 275 | + "cell_type": "code", |
| 276 | + "collapsed": false, |
| 277 | + "input": [ |
| 278 | + "def convert_cells_to_floats(csv_cont):\n", |
| 279 | + " \"\"\" \n", |
| 280 | + " Converts cells to floats if possible\n", |
| 281 | + " (modifies input CSV content list).\n", |
| 282 | + " \n", |
| 283 | + " \"\"\"\n", |
| 284 | + " for row in range(len(csv_cont)):\n", |
| 285 | + " for cell in range(len(csv_cont[row])):\n", |
| 286 | + " try:\n", |
| 287 | + " csv_cont[row][cell] = float(csv_cont[row][cell])\n", |
| 288 | + " except ValueError:\n", |
| 289 | + " pass " |
| 290 | + ], |
| 291 | + "language": "python", |
| 292 | + "metadata": {}, |
| 293 | + "outputs": [], |
| 294 | + "prompt_number": 8 |
| 295 | + }, |
| 296 | + { |
| 297 | + "cell_type": "code", |
| 298 | + "collapsed": false, |
| 299 | + "input": [ |
| 300 | + "print('first 3 rows:')\n", |
| 301 | + "for row in range(3):\n", |
| 302 | + " print(csv_cont[row])" |
| 303 | + ], |
| 304 | + "language": "python", |
| 305 | + "metadata": {}, |
| 306 | + "outputs": [ |
| 307 | + { |
| 308 | + "output_type": "stream", |
| 309 | + "stream": "stdout", |
| 310 | + "text": [ |
| 311 | + "first 3 rows:\n", |
| 312 | + "['name', 'column1', 'column2', 'column3']\n", |
| 313 | + "['abc', 1.1, 4.2, 1.2]\n", |
| 314 | + "['def', 2.1, 1.4, 5.2]\n" |
| 315 | + ] |
| 316 | + } |
| 317 | + ], |
| 318 | + "prompt_number": 9 |
249 | 319 | }, |
250 | 320 | { |
251 | 321 | "cell_type": "markdown", |
|
304 | 374 | "language": "python", |
305 | 375 | "metadata": {}, |
306 | 376 | "outputs": [], |
307 | | - "prompt_number": 5 |
| 377 | + "prompt_number": 10 |
308 | 378 | }, |
309 | 379 | { |
310 | 380 | "cell_type": "markdown", |
|
360 | 430 | ] |
361 | 431 | } |
362 | 432 | ], |
363 | | - "prompt_number": 6 |
| 433 | + "prompt_number": 11 |
364 | 434 | }, |
365 | 435 | { |
366 | 436 | "cell_type": "markdown", |
|
425 | 495 | "language": "python", |
426 | 496 | "metadata": {}, |
427 | 497 | "outputs": [], |
428 | | - "prompt_number": 28 |
| 498 | + "prompt_number": 12 |
429 | 499 | }, |
430 | 500 | { |
431 | 501 | "cell_type": "code", |
|
447 | 517 | "language": "python", |
448 | 518 | "metadata": {}, |
449 | 519 | "outputs": [], |
450 | | - "prompt_number": 29 |
| 520 | + "prompt_number": 13 |
451 | 521 | }, |
452 | 522 | { |
453 | 523 | "cell_type": "code", |
|
484 | 554 | ] |
485 | 555 | } |
486 | 556 | ], |
487 | | - "prompt_number": 32 |
| 557 | + "prompt_number": 14 |
488 | 558 | }, |
489 | 559 | { |
490 | 560 | "cell_type": "markdown", |
|
533 | 603 | "language": "python", |
534 | 604 | "metadata": {}, |
535 | 605 | "outputs": [], |
536 | | - "prompt_number": 34 |
| 606 | + "prompt_number": 15 |
537 | 607 | }, |
538 | 608 | { |
539 | 609 | "cell_type": "markdown", |
|
573 | 643 | ] |
574 | 644 | } |
575 | 645 | ], |
576 | | - "prompt_number": 35 |
| 646 | + "prompt_number": 16 |
| 647 | + }, |
| 648 | + { |
| 649 | + "cell_type": "code", |
| 650 | + "collapsed": false, |
| 651 | + "input": [], |
| 652 | + "language": "python", |
| 653 | + "metadata": {}, |
| 654 | + "outputs": [] |
577 | 655 | } |
578 | 656 | ], |
579 | 657 | "metadata": {} |
|
0 commit comments