added conversion to float

This commit is contained in:
rasbt 2014-05-14 01:51:10 -04:00
parent ed0eb89f62
commit 8f0944c7cf
3 changed files with 48 additions and 47 deletions

View File

@ -1,7 +1,7 @@
name,column1,column2,column3 name,column1,column2,column3
abc,1.1,4.2,1.2 abc,1.1,4.2,1.2
def,2.1,1.4,5.2 def,2.1,1.4,5.2
ghi,1.5,1.2,2.1 ghi,1.5,1.2,-2.1
jkl,1.8,1.1,4.2 jkl,1.8,-1.1,4.2
mno,9.4,6.6,6.2 mno,9.4,6.6,6.2
pqr,1.4,8.3,8.4 pqr,1.4,8.3,8.4

1 name column1 column2 column3
2 abc 1.1 4.2 1.2
3 def 2.1 1.4 5.2
4 ghi 1.5 1.2 2.1 -2.1
5 jkl 1.8 1.1 -1.1 4.2
6 mno 9.4 6.6 6.2
7 pqr 1.4 8.3 8.4

View File

@ -1,7 +1,7 @@
name,column1,column2,column3 name,column1,column2,column3
abc,1.1^,4.2,1.2^ abc,1.1*,4.2,1.2
def,2.1,1.4,5.2 def,2.1,1.4,5.2
ghi,1.5,1.2,2.1 ghi,1.5,1.2,-2.1*
jkl,1.8,1.1^,4.2 jkl,1.8,-1.1*,4.2
mno,9.4*,6.6,6.2 mno,9.4,6.6,6.2
pqr,1.4,8.3*,8.4* pqr,1.4,8.3,8.4

1 name column1 column2 column3
2 abc 1.1^ 1.1* 4.2 1.2^ 1.2
3 def 2.1 1.4 5.2
4 ghi 1.5 1.2 2.1 -2.1*
5 jkl 1.8 1.1^ -1.1* 4.2
6 mno 9.4* 9.4 6.6 6.2
7 pqr 1.4 8.3* 8.3 8.4* 8.4

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:13c10e46fea51bcbbf203261897bb4d8ea2087e39925e077d452778f78d62b1e" "signature": "sha256:7ce6d9e0e1dc3da5c31fc5f3a5ab7687870a76cd4adaedd6da95bc6451755b12"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -141,7 +141,7 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 4 "prompt_number": 1
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -167,7 +167,7 @@
] ]
} }
], ],
"prompt_number": 5 "prompt_number": 2
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -207,13 +207,14 @@
" \"\"\" Prints CSV file to standard output.\"\"\"\n", " \"\"\" Prints CSV file to standard output.\"\"\"\n",
" print(50*'-')\n", " print(50*'-')\n",
" for row in csv_content:\n", " for row in csv_content:\n",
" row = [str(e) for e in row]\n",
" print('\\t'.join(row))\n", " print('\\t'.join(row))\n",
" print(50*'-')" " print(50*'-')"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 6 "prompt_number": 3
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -238,15 +239,15 @@
"name\tcolumn1\tcolumn2\tcolumn3\n", "name\tcolumn1\tcolumn2\tcolumn3\n",
"abc\t1.1\t4.2\t1.2\n", "abc\t1.1\t4.2\t1.2\n",
"def\t2.1\t1.4\t5.2\n", "def\t2.1\t1.4\t5.2\n",
"ghi\t1.5\t1.2\t2.1\n", "ghi\t1.5\t1.2\t-2.1\n",
"jkl\t1.8\t1.1\t4.2\n", "jkl\t1.8\t-1.1\t4.2\n",
"mno\t9.4\t6.6\t6.2\n", "mno\t9.4\t6.6\t6.2\n",
"pqr\t1.4\t8.3\t8.4\n", "pqr\t1.4\t8.3\t8.4\n",
"--------------------------------------------------\n" "--------------------------------------------------\n"
] ]
} }
], ],
"prompt_number": 7 "prompt_number": 4
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -291,7 +292,7 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 8 "prompt_number": 5
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -310,12 +311,12 @@
"text": [ "text": [
"first 3 rows:\n", "first 3 rows:\n",
"['name', 'column1', 'column2', 'column3']\n", "['name', 'column1', 'column2', 'column3']\n",
"['abc', 1.1, 4.2, 1.2]\n", "['abc', '1.1', '4.2', '1.2']\n",
"['def', 2.1, 1.4, 5.2]\n" "['def', '2.1', '1.4', '5.2']\n"
] ]
} }
], ],
"prompt_number": 9 "prompt_number": 6
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -374,7 +375,7 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 10 "prompt_number": 7
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -393,6 +394,7 @@
"print_csv(csv_cont)\n", "print_csv(csv_cont)\n",
"\n", "\n",
"print('\\n\\nCSV sorted by column \"column3\":')\n", "print('\\n\\nCSV sorted by column \"column3\":')\n",
"convert_cells_to_floats(csv_cont)\n",
"csv_sorted = sort_by_column(csv_cont, 'column3')\n", "csv_sorted = sort_by_column(csv_cont, 'column3')\n",
"print_csv(csv_sorted)" "print_csv(csv_sorted)"
], ],
@ -410,8 +412,8 @@
"name\tcolumn1\tcolumn2\tcolumn3\n", "name\tcolumn1\tcolumn2\tcolumn3\n",
"abc\t1.1\t4.2\t1.2\n", "abc\t1.1\t4.2\t1.2\n",
"def\t2.1\t1.4\t5.2\n", "def\t2.1\t1.4\t5.2\n",
"ghi\t1.5\t1.2\t2.1\n", "ghi\t1.5\t1.2\t-2.1\n",
"jkl\t1.8\t1.1\t4.2\n", "jkl\t1.8\t-1.1\t4.2\n",
"mno\t9.4\t6.6\t6.2\n", "mno\t9.4\t6.6\t6.2\n",
"pqr\t1.4\t8.3\t8.4\n", "pqr\t1.4\t8.3\t8.4\n",
"--------------------------------------------------\n", "--------------------------------------------------\n",
@ -420,9 +422,9 @@
"CSV sorted by column \"column3\":\n", "CSV sorted by column \"column3\":\n",
"--------------------------------------------------\n", "--------------------------------------------------\n",
"name\tcolumn1\tcolumn2\tcolumn3\n", "name\tcolumn1\tcolumn2\tcolumn3\n",
"ghi\t1.5\t1.2\t-2.1\n",
"abc\t1.1\t4.2\t1.2\n", "abc\t1.1\t4.2\t1.2\n",
"ghi\t1.5\t1.2\t2.1\n", "jkl\t1.8\t-1.1\t4.2\n",
"jkl\t1.8\t1.1\t4.2\n",
"def\t2.1\t1.4\t5.2\n", "def\t2.1\t1.4\t5.2\n",
"mno\t9.4\t6.6\t6.2\n", "mno\t9.4\t6.6\t6.2\n",
"pqr\t1.4\t8.3\t8.4\n", "pqr\t1.4\t8.3\t8.4\n",
@ -430,7 +432,7 @@
] ]
} }
], ],
"prompt_number": 11 "prompt_number": 8
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -489,13 +491,13 @@
" col_index = sorted_csv[0].index(col)\n", " col_index = sorted_csv[0].index(col)\n",
" else:\n", " else:\n",
" col_index = col\n", " col_index = col\n",
" sorted_csv[1][col_index] += marker\n", " sorted_csv[1][col_index] = str(sorted_csv[1][col_index]) + marker\n",
" return None" " return None"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 12 "prompt_number": 9
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -509,7 +511,7 @@
" (modifies input CSV content list).\n", " (modifies input CSV content list).\n",
" \n", " \n",
" \"\"\"\n", " \"\"\"\n",
" for c in csv_cont[0][1:]:\n", " for c in range(1, len(csv_cont[0])):\n",
" mark_minmax(csv_cont, c, mark_max, marker)\n", " mark_minmax(csv_cont, c, mark_max, marker)\n",
" marked_csv = sort_by_column(csv_cont, 0, False)\n", " marked_csv = sort_by_column(csv_cont, 0, False)\n",
" return marked_csv" " return marked_csv"
@ -517,21 +519,21 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 13 "prompt_number": 10
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"import copy\n", "import copy\n",
"\n",
"csv_cont = csv_to_list('../Data/test.csv')\n", "csv_cont = csv_to_list('../Data/test.csv')\n",
"\n", "\n",
"csv_marked = copy.deepcopy(csv_cont)\n", "csv_marked = copy.deepcopy(csv_cont)\n",
"mark_all_col(csv_marked, mark_max=True, marker='*')\n", "convert_cells_to_floats(csv_marked)\n",
"mark_all_col(csv_marked, mark_max=False, marker='^')\n", "mark_all_col(csv_marked, mark_max=False, marker='*')\n",
"print_csv(csv_marked)\n", "print_csv(csv_marked)\n",
"\n", "print('*: min-value')"
"print('^: min-value\\n*: max-value')"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -542,19 +544,18 @@
"text": [ "text": [
"--------------------------------------------------\n", "--------------------------------------------------\n",
"name\tcolumn1\tcolumn2\tcolumn3\n", "name\tcolumn1\tcolumn2\tcolumn3\n",
"abc\t1.1^\t4.2\t1.2^\n", "abc\t1.1*\t4.2\t1.2\n",
"def\t2.1\t1.4\t5.2\n", "def\t2.1\t1.4\t5.2\n",
"ghi\t1.5\t1.2\t2.1\n", "ghi\t1.5\t1.2\t-2.1*\n",
"jkl\t1.8\t1.1^\t4.2\n", "jkl\t1.8\t-1.1*\t4.2\n",
"mno\t9.4*\t6.6\t6.2\n", "mno\t9.4\t6.6\t6.2\n",
"pqr\t1.4\t8.3*\t8.4*\n", "pqr\t1.4\t8.3\t8.4\n",
"--------------------------------------------------\n", "--------------------------------------------------\n",
"^: min-value\n", "*: min-value\n"
"*: max-value\n"
] ]
} }
], ],
"prompt_number": 14 "prompt_number": 12
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -603,7 +604,7 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 15 "prompt_number": 13
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -633,17 +634,17 @@
"Written CSV file:\n", "Written CSV file:\n",
"--------------------------------------------------\n", "--------------------------------------------------\n",
"name\tcolumn1\tcolumn2\tcolumn3\n", "name\tcolumn1\tcolumn2\tcolumn3\n",
"abc\t1.1^\t4.2\t1.2^\n", "abc\t1.1*\t4.2\t1.2\n",
"def\t2.1\t1.4\t5.2\n", "def\t2.1\t1.4\t5.2\n",
"ghi\t1.5\t1.2\t2.1\n", "ghi\t1.5\t1.2\t-2.1*\n",
"jkl\t1.8\t1.1^\t4.2\n", "jkl\t1.8\t-1.1*\t4.2\n",
"mno\t9.4*\t6.6\t6.2\n", "mno\t9.4\t6.6\t6.2\n",
"pqr\t1.4\t8.3*\t8.4*\n", "pqr\t1.4\t8.3\t8.4\n",
"--------------------------------------------------\n" "--------------------------------------------------\n"
] ]
} }
], ],
"prompt_number": 16 "prompt_number": 14
}, },
{ {
"cell_type": "code", "cell_type": "code",