{ "cells": [ { "cell_type": "markdown", "id": "74324c95", "metadata": {}, "source": [ "# Exercise 1\n", "\n", "The following will be used as an in-class exercise.\n" ] }, { "cell_type": "markdown", "id": "59cb16c6", "metadata": {}, "source": [ "1. Write a function Square that takes an integer argument and outputs the square value of this argument. For example, if the input is 3, output should be 9." ] }, { "cell_type": "code", "execution_count": null, "id": "e3f33b12", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "deccb7a7", "metadata": {}, "source": [ "2. Write a comprehensive code to initialize a dictionary where values are squares of keys – keys from 1 to 10." ] }, { "cell_type": "code", "execution_count": null, "id": "ebbe1f38", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 12, "id": "7f08fe6d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
3411Futrelle, Mrs. Jacques Heath (Lily May Peel)female35.01011380353.1000C123S
4503Allen, Mr. William Henrymale35.0003734508.0500NaNS
.......................................
88688702Montvila, Rev. Juozasmale27.00021153613.0000NaNS
88788811Graham, Miss. Margaret Edithfemale19.00011205330.0000B42S
88888903Johnston, Miss. Catherine Helen \"Carrie\"femaleNaN12W./C. 660723.4500NaNS
88989011Behr, Mr. Karl Howellmale26.00011136930.0000C148C
89089103Dooley, Mr. Patrickmale32.0003703767.7500NaNQ
\n", "

891 rows × 12 columns

\n", "
" ], "text/plain": [ " PassengerId Survived Pclass \\\n", "0 1 0 3 \n", "1 2 1 1 \n", "2 3 1 3 \n", "3 4 1 1 \n", "4 5 0 3 \n", ".. ... ... ... \n", "886 887 0 2 \n", "887 888 1 1 \n", "888 889 0 3 \n", "889 890 1 1 \n", "890 891 0 3 \n", "\n", " Name Sex Age SibSp \\\n", "0 Braund, Mr. Owen Harris male 22.0 1 \n", "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", "2 Heikkinen, Miss. Laina female 26.0 0 \n", "3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n", "4 Allen, Mr. William Henry male 35.0 0 \n", ".. ... ... ... ... \n", "886 Montvila, Rev. Juozas male 27.0 0 \n", "887 Graham, Miss. Margaret Edith female 19.0 0 \n", "888 Johnston, Miss. Catherine Helen \"Carrie\" female NaN 1 \n", "889 Behr, Mr. Karl Howell male 26.0 0 \n", "890 Dooley, Mr. Patrick male 32.0 0 \n", "\n", " Parch Ticket Fare Cabin Embarked \n", "0 0 A/5 21171 7.2500 NaN S \n", "1 0 PC 17599 71.2833 C85 C \n", "2 0 STON/O2. 3101282 7.9250 NaN S \n", "3 0 113803 53.1000 C123 S \n", "4 0 373450 8.0500 NaN S \n", ".. ... ... ... ... ... \n", "886 0 211536 13.0000 NaN S \n", "887 0 112053 30.0000 B42 S \n", "888 2 W./C. 6607 23.4500 NaN S \n", "889 0 111369 30.0000 C148 C \n", "890 0 370376 7.7500 NaN Q \n", "\n", "[891 rows x 12 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Keep this here: Loading Data. \n", "import pandas as pd\n", "df=pd.read_csv('https://raw.githubusercontent.com/rpi-techfundamentals/spring2019-materials/master/input/train.csv')\n", "df" ] }, { "cell_type": "markdown", "id": "9ff0e145", "metadata": {}, "source": [ "3. Find the median of the `age` column and assign it to the `age_median` variable. " ] }, { "cell_type": "code", "execution_count": null, "id": "f2afe9ea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "91fe499a", "metadata": {}, "source": [ "4. Count the number of NaN in the `Age` column. \n" ] }, { "cell_type": "code", "execution_count": null, "id": "6cb1d0b5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "255f0dab", "metadata": {}, "source": [ "5. Replace the NAN with the `age_median`." ] }, { "cell_type": "code", "execution_count": null, "id": "092299dd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b10f275d", "metadata": {}, "source": [ "6. Create a Pivot table which examines the survived by `Embarked` and `Sex` columns. " ] }, { "cell_type": "code", "execution_count": null, "id": "cd563d67", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b881bda1", "metadata": {}, "source": [ "### Challenge Problem\n", "\n", "6. Create a function which accepts a dataframe (`df`) and a list of columns (`cols`), and a function to use (`use` with potential values `mean` or `median` or `mode`). For each of the columns listed the function should replace NaN with the appropriate value, returning a dataframe. \n", "\n", "Add your solution to this notebook:\n", "\n", "https://colab.research.google.com/drive/1QDeA-aIjC9o2f638Hmhu_xHBQf3W1CXs?usp=sharing\n", "\n", "Put your name. " ] }, { "cell_type": "code", "execution_count": null, "id": "65b231dd", "metadata": {}, "outputs": [], "source": [] } ], "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.7.10" } }, "nbformat": 4, "nbformat_minor": 5 }