{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1cc1bdae-fb8c-4df5-bceb-e966ef5872c5",
   "metadata": {},
   "source": [
    "## Introduction to Data Science\n",
    "\n",
    "#### University of Redlands - DATA 101\n",
    "#### Prof: Joanna Bieri [joanna_bieri@redlands.edu](mailto:joanna_bieri@redlands.edu)\n",
    "#### [Class Website: data101.joannabieri.com](https://data101.joannabieri.com)\n",
    "\n",
    "---------------------------------------\n",
    "# Homework Day 2\n",
    "---------------------------------------\n",
    "\n",
    "GOALS:\n",
    "\n",
    "1. Start using Python\n",
    "2. Start looking at Data!\n",
    "\n",
    "----------------------------------------------------------\n",
    "\n",
    "This homework has **TWO** warm-up problems and **FOUR** problems."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2c39d80-a1fd-479b-910c-6ad341ad5310",
   "metadata": {},
   "source": [
    "\n",
    "## Hello World!\n",
    "\n",
    "### Warm up Problem 1\n",
    "\n",
    "Write python code that will print your name and run the cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a219b6a-b87f-456d-b991-c392fe5cc5b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2b797c2-cfcd-4a3f-acf2-3ad6f0183192",
   "metadata": {},
   "source": [
    "## Installing Modules\n",
    "\n",
    "### Warm up Problem 2\n",
    "\n",
    "You already installed everything you need for the whole semester when you ran `pip install -r requirements.txt` during Day 1 setup - nothing to install here! Just run the cell below to confirm."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56b03eae-6df6-424b-8325-0f40ca79e8c1",
   "metadata": {},
   "source": [
    "## Let's do some Data Science.\n",
    "\n",
    "We will explore some data about how countries vote at the United Nations General Assembly. You should follow along with the notes and/or class video.\n",
    "\n",
    "I am breaking this into parts and having you copy and paste the code so you can start to identify what the different parts of the code do."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b375e170-9404-4a45-b5ac-9896c009fdff",
   "metadata": {},
   "source": [
    "-------------------------------\n",
    "### Importing Packages:\n",
    "-------------------------------\n",
    "\n",
    "Every time we start a new project we will import the packages that will help us do the analysis. Copy and paste all of the imports in the cell below.\n",
    "\n",
    "* numpy = mathematical and number packages for python\n",
    "* pandas = pretty tables, dataframes, and data analysis packages\n",
    "* matplotlib.pyplot = nice looking graphs\n",
    "* plotly = nice looking graphs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d377d0c6-d6b9-4bbf-9561-02402781a58d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copy and paste the code here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d0f1b4e-0edd-4c16-a40b-fe13d095e15c",
   "metadata": {},
   "source": [
    "-------------------------------\n",
    "### Get the Data:\n",
    "-------------------------------\n",
    "\n",
    "We will practice all sorts of ways to get data into our notebooks. When you use Pandas (pd) to read a csv:\n",
    "\n",
    "    DF = pd.read_csv(file_location)\n",
    "\n",
    "you are basically reading in a spreadsheet (Excel). The data for today is stored on my website.\n",
    "\n",
    "It might take a minute for the data to load."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1253d511-c553-454f-a94b-2d0a6f97bd1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copy and paste the code here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4775aa78-8ca3-4d32-bf28-ea0f2eff5650",
   "metadata": {},
   "source": [
    "\n",
    "-------------------------------\n",
    "### Initial Data Exploration:\n",
    "-------------------------------\n",
    "-------------------------------\n",
    "### Show the Data:\n",
    "-------------------------------\n",
    "\n",
    "We want to look at what we have. The data is now represented by the variable DF."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da556aed-fa86-4049-bc8b-9cfc8c3666b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copy and paste the code here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3fac6d0-283f-45a8-8f04-824bce2bc030",
   "metadata": {},
   "source": [
    "### Problem 1\n",
    "\n",
    "Do some initial exploration of this data and write about it in the markdown cell below.\n",
    "\n",
    "* How man columns are there?\n",
    "* What do the columns represent?\n",
    "* What countries are there?\n",
    "* Can you search for a country you are interested in?\n",
    "* How many rows are there?\n",
    "* What other observations do you have?\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d647175-19fd-404b-84e2-b92e627413a1",
   "metadata": {},
   "source": [
    "**(Double Click Here)**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5ec908c-b134-4b31-907d-93f4415315b4",
   "metadata": {},
   "source": [
    "-------------------------------\n",
    "### Make Python Explore the Data!:\n",
    "-------------------------------\n",
    "\n",
    "Copy and paste the command that will show you a list of the counties:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76ada79b-d012-48ff-bfe5-b110ce00ac4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copy and paste the code here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6869f91-d574-4e4f-8200-7f7ba12ef657",
   "metadata": {},
   "source": [
    "Copy and paste the command that will count up the number of countries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa93e932-08e9-46b8-ae03-1f6504a8d7b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copy and paste the code here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fc80b9c-3777-4ed3-903f-610f9a889561",
   "metadata": {},
   "source": [
    "### Problem 2\n",
    "\n",
    "Try writing some code for your self. Above we found a countries list by focusing on the column named 'country'. See if you can redo that same code but change it to focus on the column named 'issue'.\n",
    "\n",
    "What do I expect here:\n",
    "\n",
    "* First copy and past the code from above\n",
    "* Then change that code slightly\n",
    "* Run the cell to see if it worked"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8322841e-daef-4ff3-a74b-20560af34c52",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ebdffc3-dbc2-4100-83d6-ea94d27f9456",
   "metadata": {},
   "source": [
    "-------------------------------\n",
    "### Data Visualization:\n",
    "-------------------------------\n",
    "\n",
    "Now we will select three countries that we are interested in and see how their votes have changed over time. Below you should see code that selects: Turkey, United States, and United Kingdom.\n",
    "\n",
    "**IMPORTANT** These have to be spelled and capitalized exactly like they are in the data. Python is unforgiving of typos!\n",
    "\n",
    "You can just run the cell below - assuming you have done all the parts above!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb0dfbd6-1959-4881-890a-aa184248699d",
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = ['Turkey', 'United States', 'United Kingdom']\n",
    "issues = list(DF['issue'].unique())\n",
    "c_groups = DF.groupby(['country','issue'])\n",
    "print(issues)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00a813fa-de2b-4c25-a96c-68234b9b9503",
   "metadata": {},
   "source": [
    "Now that we have our focus countries, we can make pretty pictures.\n",
    "\n",
    "You can just run the cell below - assuming you have done all the parts above!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0004cb71-264f-4580-b847-41f8b01fa300",
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_plot(countries,issue):\n",
    "    '''\n",
    "    A Python function that takes in the list of countries and issues and makes\n",
    "    a scatter plot of each issue with a trendline for each country.\n",
    "    '''\n",
    "    x_data = []\n",
    "    y_data = []\n",
    "    c_data = []\n",
    "    for cntry in countries:\n",
    "        my_group = c_groups.get_group((cntry,issue))\n",
    "        for y in my_group['year'].unique():\n",
    "            x_data.append(y)\n",
    "            tot_yes = sum(my_group[my_group['year']==y]['vote']=='yes')\n",
    "            percent_yes = tot_yes/len(my_group[my_group['year']==y])*100\n",
    "            y_data.append(percent_yes)\n",
    "            c_data.append(cntry)\n",
    "\n",
    "    fig = px.scatter(x=x_data, y=y_data,color=c_data,trendline=\"lowess\",labels={\"color\": \"Country\"})\n",
    "\n",
    "    fig.update_layout(\n",
    "        title={\n",
    "            'text': issue + '<br>',\n",
    "            'y':0.9,\n",
    "            'x':0.5,\n",
    "            'xanchor': 'center',\n",
    "            'yanchor': 'top'})\n",
    "    fig.update_yaxes(title_text=\"% Yes\")\n",
    "    fig.update_xaxes(title_text=\"Year\")\n",
    "    fig.show()\n",
    "    \n",
    "for iss in issues:\n",
    "    make_plot(countries,iss)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "130de118-e079-455c-9eb2-0eff64efca6a",
   "metadata": {},
   "source": [
    "### Problem 3\n",
    "\n",
    "Choose one of the graphs produced above (should match the lecture notes) and discuss what you see there:\n",
    "\n",
    "1. Which issue are you focusing on.\n",
    "2. What do the graph axis represent?\n",
    "3. Say in words how the United States votes have changed over time.\n",
    "4. How do the United States votes compare to the other countries?\n",
    "5. See if you can come up with one or two questions that interest you about the data in the graph."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de5b34d4-b795-43ac-a6e0-9c66680beb68",
   "metadata": {},
   "source": [
    "**(double click here)**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4874978e-edb6-488a-a11e-6dd290b351cc",
   "metadata": {},
   "source": [
    "### Problem 4\n",
    "\n",
    "Now go back up to where we picked our three focus countries and choose some different ones. Rerun the analysis. If you want you could choose more than three countries. \n",
    "\n",
    "Discuss the graph for the same issue but with the new countries."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed39a439-8ef9-4d66-9a84-4d1615f971b5",
   "metadata": {},
   "source": [
    "**(double click here)**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fcce607-bfda-42a1-a556-a05c424dd6c9",
   "metadata": {},
   "source": [
    "--------------------------------\n",
    "### You are done with the homework... now what?\n",
    "--------------------------------\n",
    "\n",
    "1. Save your changes.\n",
    "2. In the Git tab, **Stage** your changed files - use the (+) button next to the file.\n",
    "3. **Commit** your changes by entering a summary and pushing commit.\n",
    "4. **Push** your changes using the cloud button.\n",
    "5. Check that your changes are on your GitHub repo (online)\n",
    "6. **TAKE THE DAILY QUIZ ON CANVAS**.\n",
    "7. Come to class and get your questions answered.\n",
    "\n",
    "At the end of the week, whatever is in your repo at the deadline is what gets graded - there is no separate submission step. If it's pushed, it's submitted.\n",
    "\n",
    "**IMPORTANT** If this is confusing this first week, don't worry, I can help you and I will be really flexible about these first few deadlines.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f00c8fd3-9713-4a3c-906e-7db71731da15",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}