diff --git a/tutorials/sorting_csvs.ipynb b/tutorials/sorting_csvs.ipynb index 04e99ab..237db52 100644 --- a/tutorials/sorting_csvs.ipynb +++ b/tutorials/sorting_csvs.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:683cfd7e6b5fd1dca9cd2028b294ec8ae44a175b613eb3c037d365a28957a987" + "signature": "sha256:13c10e46fea51bcbbf203261897bb4d8ea2087e39925e077d452778f78d62b1e" }, "nbformat": 3, "nbformat_minor": 0, @@ -56,6 +56,7 @@ "## Sections\n", "- [Reading in a CSV file](#reading)\n", "- [Printing the CSV file contents](#printing)\n", + "- [Converting numeric cells to floats](#floats)\n", "- [Sorting the CSV file](#sorting)\n", "- [Marking min/max values in particular columns](#marking)\n", "- [Writing out the modified table to as a new CSV file](#writing)\n", @@ -140,7 +141,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 1 + "prompt_number": 4 }, { "cell_type": "code", @@ -166,7 +167,7 @@ ] } ], - "prompt_number": 3 + "prompt_number": 5 }, { "cell_type": "markdown", @@ -212,7 +213,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 4 + "prompt_number": 6 }, { "cell_type": "code", @@ -245,7 +246,76 @@ ] } ], - "prompt_number": 5 + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "
\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Converting numeric cells to floats" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def convert_cells_to_floats(csv_cont):\n", + " \"\"\" \n", + " Converts cells to floats if possible\n", + " (modifies input CSV content list).\n", + " \n", + " \"\"\"\n", + " for row in range(len(csv_cont)):\n", + " for cell in range(len(csv_cont[row])):\n", + " try:\n", + " csv_cont[row][cell] = float(csv_cont[row][cell])\n", + " except ValueError:\n", + " pass " + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print('first 3 rows:')\n", + "for row in range(3):\n", + " print(csv_cont[row])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "first 3 rows:\n", + "['name', 'column1', 'column2', 'column3']\n", + "['abc', 1.1, 4.2, 1.2]\n", + "['def', 2.1, 1.4, 5.2]\n" + ] + } + ], + "prompt_number": 9 }, { "cell_type": "markdown", @@ -304,7 +374,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 5 + "prompt_number": 10 }, { "cell_type": "markdown", @@ -360,7 +430,7 @@ ] } ], - "prompt_number": 6 + "prompt_number": 11 }, { "cell_type": "markdown", @@ -425,7 +495,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 28 + "prompt_number": 12 }, { "cell_type": "code", @@ -447,7 +517,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 29 + "prompt_number": 13 }, { "cell_type": "code", @@ -484,7 +554,7 @@ ] } ], - "prompt_number": 32 + "prompt_number": 14 }, { "cell_type": "markdown", @@ -533,7 +603,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 34 + "prompt_number": 15 }, { "cell_type": "markdown", @@ -573,7 +643,15 @@ ] } ], - "prompt_number": 35 + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {}