{ "cells": [ { "cell_type": "markdown", "id": "6073eb74", "metadata": {}, "source": [ "# In Class Activity - More Pandas and data processing" ] }, { "cell_type": "markdown", "id": "1f1a34fa", "metadata": {}, "source": [ "First, let's import our packages." ] }, { "cell_type": "code", "execution_count": null, "id": "27fec022", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import numpy.random as npr\n", "import math" ] }, { "cell_type": "markdown", "id": "6d5c7ba4", "metadata": {}, "source": [ "Second, we want to read in a csv file in the dataframe `df`. The file has a list of various faculty members and their phone numbers (don't worry, the phone numbers are randomly generated. So best not try calling them). We also want to tell `df` that the phone numbers are actually strings rather than traditional numbers, which we do with the `astype` method." ] }, { "cell_type": "code", "execution_count": null, "id": "193b2fdb", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('faculty.csv')\n", "df['Phone'] = df['Phone'].astype(str)\n", "df" ] }, { "cell_type": "markdown", "id": "bd755d44", "metadata": {}, "source": [ "### Problem 0: Changing the format of a phone number" ] }, { "cell_type": "markdown", "id": "67e11322", "metadata": {}, "source": [ "For this problem, we want to convert the phone numbers to a more readable format [ 9285162643 $\\rightarrow$ (928)516-2643 ]. You may have experience doing this sort of thing by hand with Excel, which can be very cumbersome and error-prone. Let's see how to do this with pandas instead.\n", "\n", "Please write code to make the transformation to (ABC)DEF-HIJK format for each phone number in `df`.\n", "\n", "_Hint_: Write a function `convert_phone` that converts the format of a single phone number (also, remind yourself about Python list slicing). Then, you can apply that function using the `transform` operation described in book section 6.12.1.2." ] }, { "cell_type": "code", "execution_count": null, "id": "9a0496aa", "metadata": {}, "outputs": [], "source": [ "# Your answer goes here" ] }, { "cell_type": "markdown", "id": "cec6b441", "metadata": {}, "source": [ "### Problem 1: Making a new column" ] }, { "cell_type": "markdown", "id": "2dcb7d61", "metadata": {}, "source": [ "Using the same dataframe `df`, make a new column that lists the complete name of each professor. For instance, the new column should be called 'Complete name' and the first entry should be the string 'Karen Adolph'.\n", "\n", "_Hint_: You could make a new function and use the same logic as above. Alternatively, you can also try directly summing the relevant columns." ] }, { "cell_type": "code", "execution_count": null, "id": "d9b3d082", "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "id": "a0c2970a", "metadata": {}, "source": [ "### Problem 2: Computing mean by group" ] }, { "cell_type": "markdown", "id": "5638e223", "metadata": {}, "source": [ "Let's create the rectangle and circle dataframe from last week and call it `df_shapes`." ] }, { "cell_type": "code", "execution_count": null, "id": "c7e592c2", "metadata": {}, "outputs": [], "source": [ "mytype = np.array(['rectangle','circle','rectangle','rectangle','circle','rectangle','circle','rectangle','circle','circle'])\n", "width = npr.rand(len(mytype))*10.\n", "height = npr.rand(len(mytype))*10.\n", "height[mytype=='circle']=np.nan \n", "df_shapes = pd.DataFrame({\"type\":mytype, \"width\":width, \"height\":height})\n", "df_shapes" ] }, { "cell_type": "markdown", "id": "7b90d30d", "metadata": {}, "source": [ "Next, you should compute the mean 'width' separately for the rectangles and circles.\n", "\n", "_Hint_: you can use `groupby` and `.mean()` from chapter 6.12." ] }, { "cell_type": "code", "execution_count": null, "id": "a7bd9e91", "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 5 }