Skip to content

Commit ed0eb89

Browse files
committed
added conversion to float
1 parent e43265c commit ed0eb89

1 file changed

Lines changed: 90 additions & 12 deletions

File tree

tutorials/sorting_csvs.ipynb

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:683cfd7e6b5fd1dca9cd2028b294ec8ae44a175b613eb3c037d365a28957a987"
4+
"signature": "sha256:13c10e46fea51bcbbf203261897bb4d8ea2087e39925e077d452778f78d62b1e"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -56,6 +56,7 @@
5656
"## Sections\n",
5757
"- [Reading in a CSV file](#reading)\n",
5858
"- [Printing the CSV file contents](#printing)\n",
59+
"- [Converting numeric cells to floats](#floats)\n",
5960
"- [Sorting the CSV file](#sorting)\n",
6061
"- [Marking min/max values in particular columns](#marking)\n",
6162
"- [Writing out the modified table to as a new CSV file](#writing)\n",
@@ -140,7 +141,7 @@
140141
"language": "python",
141142
"metadata": {},
142143
"outputs": [],
143-
"prompt_number": 1
144+
"prompt_number": 4
144145
},
145146
{
146147
"cell_type": "code",
@@ -166,7 +167,7 @@
166167
]
167168
}
168169
],
169-
"prompt_number": 3
170+
"prompt_number": 5
170171
},
171172
{
172173
"cell_type": "markdown",
@@ -212,7 +213,7 @@
212213
"language": "python",
213214
"metadata": {},
214215
"outputs": [],
215-
"prompt_number": 4
216+
"prompt_number": 6
216217
},
217218
{
218219
"cell_type": "code",
@@ -245,7 +246,76 @@
245246
]
246247
}
247248
],
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
249319
},
250320
{
251321
"cell_type": "markdown",
@@ -304,7 +374,7 @@
304374
"language": "python",
305375
"metadata": {},
306376
"outputs": [],
307-
"prompt_number": 5
377+
"prompt_number": 10
308378
},
309379
{
310380
"cell_type": "markdown",
@@ -360,7 +430,7 @@
360430
]
361431
}
362432
],
363-
"prompt_number": 6
433+
"prompt_number": 11
364434
},
365435
{
366436
"cell_type": "markdown",
@@ -425,7 +495,7 @@
425495
"language": "python",
426496
"metadata": {},
427497
"outputs": [],
428-
"prompt_number": 28
498+
"prompt_number": 12
429499
},
430500
{
431501
"cell_type": "code",
@@ -447,7 +517,7 @@
447517
"language": "python",
448518
"metadata": {},
449519
"outputs": [],
450-
"prompt_number": 29
520+
"prompt_number": 13
451521
},
452522
{
453523
"cell_type": "code",
@@ -484,7 +554,7 @@
484554
]
485555
}
486556
],
487-
"prompt_number": 32
557+
"prompt_number": 14
488558
},
489559
{
490560
"cell_type": "markdown",
@@ -533,7 +603,7 @@
533603
"language": "python",
534604
"metadata": {},
535605
"outputs": [],
536-
"prompt_number": 34
606+
"prompt_number": 15
537607
},
538608
{
539609
"cell_type": "markdown",
@@ -573,7 +643,15 @@
573643
]
574644
}
575645
],
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": []
577655
}
578656
],
579657
"metadata": {}

0 commit comments

Comments
 (0)