{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" }, "tags": [ "setup" ] }, "source": [ "(c) 2016 - present. Enplus Advisors, Inc." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" }, "tags": [ "setup" ] }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [ "setup" ] }, "outputs": [], "source": [ "df = pd.DataFrame({\n", " 'ticker': ['AAPL', 'AAPL', 'MSFT', 'IBM', 'YHOO'],\n", " 'date': ['2015-12-30', '2015-12-31', '2015-12-30', '2015-12-30', '2015-12-30'],\n", " 'open': [426.23, 427.81, 42.3, 101.65, 35.53]\n", "})" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "* Select the `open` column as a `Series` using attribute lookup\n", "* Select the `open` column as a `Series` using `dict`-style lookup\n", "* Select the `date` column as a `DataFrame`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "res1a = df.open\n", "res1b = df['open']\n", "res1c = df[['open']]" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "* Select all rows with the `AAPL` ticker and the `date` and `open`\n", " columns.\n", "* Assign to the variable `df1` a new `DataFrame` with `ticker` as\n", " the index.\n", "* Assign to the variable `df2` a new `DataFrame` with `date` as\n", " the index. Create this `DataFrame` from `df1` with a single\n", " statement.\n", "* Sort `df2` by the index values." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "res2a = df.loc[df.ticker == 'AAPL', ['date', 'open']]\n", "df1 = df.set_index('ticker')\n", "df2 = df1.reset_index().set_index('date')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "df2_sorted = df2.sort_index()" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exercise" ] }, "source": [ "**Exercise:**\n", "\n", "* Create a copy of `df` called `df3`. Add a new column of `NaNs` \n", " to `df3` called `close`. Assign `close` the same value as `open`\n", " for all `open` values greater than 100.\n", "* Sort `df3` by its `close` values." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | ticker | \n", "date | \n", "open | \n", "close | \n", "
|---|---|---|---|---|
| 0 | \n", "AAPL | \n", "2015-12-30 | \n", "426.23 | \n", "426.23 | \n", "
| 1 | \n", "AAPL | \n", "2015-12-31 | \n", "427.81 | \n", "427.81 | \n", "
| 2 | \n", "MSFT | \n", "2015-12-30 | \n", "42.30 | \n", "NaN | \n", "
| 3 | \n", "IBM | \n", "2015-12-30 | \n", "101.65 | \n", "101.65 | \n", "
| 4 | \n", "YHOO | \n", "2015-12-30 | \n", "35.53 | \n", "NaN | \n", "