{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Denison CS181/DA210 Homework\n", "\n", "Before you turn this problem in, make sure everything runs as expected. This is a combination of **restarting the kernel** and then **running all cells** (in the menubar, select Kernel$\\rightarrow$Restart And Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1:** Convert the following into a data frame and name it `df`: \n", "\n", " [[1, 2, 3],[4, 5, 6],[7, 8, 9],\n", " [10, 11, 12],[13, 14, 15],[16, 17, 18]]\n", "\n", "Then display the data frame." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "ba11c34ff7129a5ad3bfcf537164848f", "grade": false, "grade_id": "cell-4936305b37b4ed31", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "cc66688370c8b79948a916306882fe58", "grade": true, "grade_id": "cell-4957ce7849139538", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert list(df[0])==[1, 4, 7, 10, 13, 16]\n", "assert list(df[2])==[3, 6, 9, 12, 15, 18]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2:** With reference to the above, assign the columns the following labels: 'A', 'B', and 'C'.\n", "\n", "Assign the rows the following labels: 'U','V','W','X','Y', and 'Z'\n", "\n", "The result should still be `df`. Then, display the data frame.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "32e62405f10c140c5d1b4532558bd258", "grade": false, "grade_id": "cell-f5c50ce855088830", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "7b1b1d625d32e472009ae0ab187ca731", "grade": true, "grade_id": "cell-68b87801c6087f1b", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert list(df['A'])==[1, 4, 7, 10, 13, 16]\n", "assert list(df['C'])==[3, 6, 9, 12, 15, 18]\n", "assert list(df.loc['X'])==[10, 11, 12]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3:** In two steps:\n", "\n", "- Project the first two columns and assign the result to `df1`\n", "- From `df1` select all the rows from index 3 onward and assign the result to `df2`\n", "\n", "Then display the result.\n", "\n", "Think about how you might accomplish the same in one line." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "c1b561fa8cef0b1078d7f573bb707fd0", "grade": false, "grade_id": "cell-350eadd927920816", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "cd37c53f87c18a8d85c7615f441c6913", "grade": true, "grade_id": "cell-f053c2df6f936901", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert list(df2['A'])==[10, 13, 16]\n", "assert list(df2['B'])==[11, 14, 17]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q5:** Convert the following dictionary (a DoL representation) to a data frame called `df` and then assign to variable `test1` the column vector associated with the scores of **Test1**.\n", "\n", " {'Name' : ['Owen', 'Rachel', 'Kyle'], \n", " 'Test1' : [82, 85, 95], \n", " 'Test2' : [89, 85, 90], \n", " 'Test3' : [93, 85, 87]}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "d6233a00a8efbb82120e59e2fe3f57ed", "grade": false, "grade_id": "cell-3ec9b06343f7d5d6", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "3f8f6f2410c8a997ceac21529f455aa1", "grade": true, "grade_id": "cell-bacb0663343d7f7c", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert df.shape == (3,4)\n", "assert isinstance(test1, pd.core.series.Series)\n", "assert list(test1) == [82, 85, 95]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q6:** Print the following:\n", "\n", "1. The test scores of Test1 (as a Series)\n", "2. Rachel's test scores (as a one-row DataFrame)\n", "3. Kyle's third test score (as a scalar integer)\n", "4. Using indexing, the scores of Owen and Rachel (as a two-row DataFrame)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "78f0c814a7656f293bf568ffddd3a0fe", "grade": true, "grade_id": "cell-15d0798550e374e5", "locked": false, "points": 4, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q7**: Compute a column Series, assigned to `final`, that equals the average of the three test scores." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "18fc48338bedeb9e3c944f27a55f07a8", "grade": false, "grade_id": "cell-f52532d3db1fb99c", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "final" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "006f52dfff6c96c16b457645efef6184", "grade": true, "grade_id": "cell-402ed44a99667b38", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert isinstance(final, pd.core.series.Series)\n", "assert list(final)[:2] == [88.0, 85.0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q8** Use the following data to create a DataFrame named `df`:\n", "\n", "\n", " data = {'animal': ['cat','cat','snake','dog',\n", " 'dog','cat','snake','cat','dog','dog'],\n", " 'age': [2.5, 3, 0.5, 7, 5, 2, 4.5, 4, 7, 3],\n", " 'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],\n", " 'priority': ['yes','yes','no','yes','no', \n", " 'no','no','yes','no', 'no']}\n", "\n", "Using as index:\n", "\n", " labels = ['a', 'b', 'c', 'd', 'e', \n", " 'f', 'g', 'h', 'i', 'j']\n", "\n", "Then, carry out a selection of rows `d` through `g`, inclusive using row labels (Index elements), not positions. Assign the result to `df2`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "e33ac59cd4ab09686fe7e256d4f93c22", "grade": false, "grade_id": "cell-61eb39ee4ca8447a", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "ad92f3d9010c0ad6ba26bbf002caa963", "grade": true, "grade_id": "cell-f2ba02aa5279c53e", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert df.shape == (10,4)\n", "assert df2.shape == (4,4)\n", "assert 'd' in df2.index\n", "assert 'g' in df2.index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q9:** In a single assignment statement, select the data in rows 2, 3, and 4 and project the columns 'animal' and 'age', assigning to `df3`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "31bc9ad69b16727d1148831b4a3fbb5a", "grade": false, "grade_id": "cell-37270ca1f13d69c2", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "df3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "e518557eb79008df6f51979624b94cbe", "grade": true, "grade_id": "cell-638458b2a637a019", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert df3.shape == (3, 2)\n", "assert 'c' in df3.index\n", "assert 'e' in df3.index\n", "assert 'animal' in df3.columns\n", "assert 'age' in df3.columns" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }