{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How do I use the MultiIndex in pandas? ([video](https://www.youtube.com/watch?v=tcRGa2soc-c&list=PL5-da3qGB5ICCsgW1MxlZ0Hq8LL5U3u9y&index=31))\n", "\n", "- [My pandas video series](https://www.dataschool.io/easier-data-analysis-with-pandas/)\n", "- [GitHub repository](https://github.com/justmarkham/pandas-videos)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "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", "
DateCloseVolumeSymbol
02016-10-0331.5014070500CSCO
12016-10-03112.5221701800AAPL
22016-10-0357.4219189500MSFT
32016-10-04113.0029736800AAPL
42016-10-0457.2420085900MSFT
52016-10-0431.3518460400CSCO
62016-10-0557.6416726400MSFT
72016-10-0531.5911808600CSCO
82016-10-05113.0521453100AAPL
\n", "
" ], "text/plain": [ " Date Close Volume Symbol\n", "0 2016-10-03 31.50 14070500 CSCO\n", "1 2016-10-03 112.52 21701800 AAPL\n", "2 2016-10-03 57.42 19189500 MSFT\n", "3 2016-10-04 113.00 29736800 AAPL\n", "4 2016-10-04 57.24 20085900 MSFT\n", "5 2016-10-04 31.35 18460400 CSCO\n", "6 2016-10-05 57.64 16726400 MSFT\n", "7 2016-10-05 31.59 11808600 CSCO\n", "8 2016-10-05 113.05 21453100 AAPL" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks = pd.read_csv('data/stocks.csv')\n", "stocks" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RangeIndex(start=0, stop=9, step=1)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.index" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Symbol\n", "AAPL 112.856667\n", "CSCO 31.480000\n", "MSFT 57.433333\n", "Name: Close, dtype: float64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.groupby('Symbol').Close.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Series with MultiIndex" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Symbol Date \n", "AAPL 2016-10-03 112.52\n", " 2016-10-04 113.00\n", " 2016-10-05 113.05\n", "CSCO 2016-10-03 31.50\n", " 2016-10-04 31.35\n", " 2016-10-05 31.59\n", "MSFT 2016-10-03 57.42\n", " 2016-10-04 57.24\n", " 2016-10-05 57.64\n", "Name: Close, dtype: float64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser = stocks.groupby(['Symbol', 'Date']).Close.mean()\n", "ser" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MultiIndex(levels=[['AAPL', 'CSCO', 'MSFT'], ['2016-10-03', '2016-10-04', '2016-10-05']],\n", " codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2]],\n", " names=['Symbol', 'Date'])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser.index" ] }, { "cell_type": "code", "execution_count": 7, "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", "
Date2016-10-032016-10-042016-10-05
Symbol
AAPL112.52113.00113.05
CSCO31.5031.3531.59
MSFT57.4257.2457.64
\n", "
" ], "text/plain": [ "Date 2016-10-03 2016-10-04 2016-10-05\n", "Symbol \n", "AAPL 112.52 113.00 113.05\n", "CSCO 31.50 31.35 31.59\n", "MSFT 57.42 57.24 57.64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser.unstack()" ] }, { "cell_type": "code", "execution_count": 8, "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", "
Date2016-10-032016-10-042016-10-05
Symbol
AAPL112.52113.00113.05
CSCO31.5031.3531.59
MSFT57.4257.2457.64
\n", "
" ], "text/plain": [ "Date 2016-10-03 2016-10-04 2016-10-05\n", "Symbol \n", "AAPL 112.52 113.00 113.05\n", "CSCO 31.50 31.35 31.59\n", "MSFT 57.42 57.24 57.64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = stocks.pivot_table(values='Close', index='Symbol', columns='Date')\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selection from Series with MultiIndex" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Symbol Date \n", "AAPL 2016-10-03 112.52\n", " 2016-10-04 113.00\n", " 2016-10-05 113.05\n", "CSCO 2016-10-03 31.50\n", " 2016-10-04 31.35\n", " 2016-10-05 31.59\n", "MSFT 2016-10-03 57.42\n", " 2016-10-04 57.24\n", " 2016-10-05 57.64\n", "Name: Close, dtype: float64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "Date\n", "2016-10-03 112.52\n", "2016-10-04 113.00\n", "2016-10-05 113.05\n", "Name: Close, dtype: float64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser.loc['AAPL']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "112.52" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser.loc['AAPL', '2016-10-03']" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "Symbol\n", "AAPL 112.52\n", "CSCO 31.50\n", "MSFT 57.42\n", "Name: Close, dtype: float64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser.loc[:, '2016-10-03']" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": true }, "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", "
Date2016-10-032016-10-042016-10-05
Symbol
AAPL112.52113.00113.05
CSCO31.5031.3531.59
MSFT57.4257.2457.64
\n", "
" ], "text/plain": [ "Date 2016-10-03 2016-10-04 2016-10-05\n", "Symbol \n", "AAPL 112.52 113.00 113.05\n", "CSCO 31.50 31.35 31.59\n", "MSFT 57.42 57.24 57.64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "Date\n", "2016-10-03 112.52\n", "2016-10-04 113.00\n", "2016-10-05 113.05\n", "Name: AAPL, dtype: float64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc['AAPL']" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "112.52" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc['AAPL', '2016-10-03']" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Symbol\n", "AAPL 112.52\n", "CSCO 31.50\n", "MSFT 57.42\n", "Name: 2016-10-03, dtype: float64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[:, '2016-10-03']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DataFrame with MultiIndex" ] }, { "cell_type": "code", "execution_count": 17, "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", "
CloseVolume
SymbolDate
CSCO2016-10-0331.5014070500
AAPL2016-10-03112.5221701800
MSFT2016-10-0357.4219189500
AAPL2016-10-04113.0029736800
MSFT2016-10-0457.2420085900
CSCO2016-10-0431.3518460400
MSFT2016-10-0557.6416726400
CSCO2016-10-0531.5911808600
AAPL2016-10-05113.0521453100
\n", "
" ], "text/plain": [ " Close Volume\n", "Symbol Date \n", "CSCO 2016-10-03 31.50 14070500\n", "AAPL 2016-10-03 112.52 21701800\n", "MSFT 2016-10-03 57.42 19189500\n", "AAPL 2016-10-04 113.00 29736800\n", "MSFT 2016-10-04 57.24 20085900\n", "CSCO 2016-10-04 31.35 18460400\n", "MSFT 2016-10-05 57.64 16726400\n", "CSCO 2016-10-05 31.59 11808600\n", "AAPL 2016-10-05 113.05 21453100" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.set_index(['Symbol', 'Date'], inplace=True)\n", "stocks" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MultiIndex(levels=[['AAPL', 'CSCO', 'MSFT'], ['2016-10-03', '2016-10-04', '2016-10-05']],\n", " codes=[[1, 0, 2, 0, 2, 1, 2, 1, 0], [0, 0, 0, 1, 1, 1, 2, 2, 2]],\n", " names=['Symbol', 'Date'])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.index" ] }, { "cell_type": "code", "execution_count": 19, "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", "
CloseVolume
SymbolDate
AAPL2016-10-03112.5221701800
2016-10-04113.0029736800
2016-10-05113.0521453100
CSCO2016-10-0331.5014070500
2016-10-0431.3518460400
2016-10-0531.5911808600
MSFT2016-10-0357.4219189500
2016-10-0457.2420085900
2016-10-0557.6416726400
\n", "
" ], "text/plain": [ " Close Volume\n", "Symbol Date \n", "AAPL 2016-10-03 112.52 21701800\n", " 2016-10-04 113.00 29736800\n", " 2016-10-05 113.05 21453100\n", "CSCO 2016-10-03 31.50 14070500\n", " 2016-10-04 31.35 18460400\n", " 2016-10-05 31.59 11808600\n", "MSFT 2016-10-03 57.42 19189500\n", " 2016-10-04 57.24 20085900\n", " 2016-10-05 57.64 16726400" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.sort_index(inplace=True)\n", "stocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selection from DataFrame with MultiIndex" ] }, { "cell_type": "code", "execution_count": 20, "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", "
CloseVolume
Date
2016-10-03112.5221701800
2016-10-04113.0029736800
2016-10-05113.0521453100
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2016-10-03 112.52 21701800\n", "2016-10-04 113.00 29736800\n", "2016-10-05 113.05 21453100" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc['AAPL']" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Close 112.52\n", "Volume 21701800.00\n", "Name: (AAPL, 2016-10-03), dtype: float64" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc[('AAPL', '2016-10-03'), :]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "112.52" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc[('AAPL', '2016-10-03'), 'Close']" ] }, { "cell_type": "code", "execution_count": 23, "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", "
CloseVolume
SymbolDate
AAPL2016-10-03112.5221701800
2016-10-04113.0029736800
2016-10-05113.0521453100
MSFT2016-10-0357.4219189500
2016-10-0457.2420085900
2016-10-0557.6416726400
\n", "
" ], "text/plain": [ " Close Volume\n", "Symbol Date \n", "AAPL 2016-10-03 112.52 21701800\n", " 2016-10-04 113.00 29736800\n", " 2016-10-05 113.05 21453100\n", "MSFT 2016-10-03 57.42 19189500\n", " 2016-10-04 57.24 20085900\n", " 2016-10-05 57.64 16726400" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc[['AAPL', 'MSFT'], :]" ] }, { "cell_type": "code", "execution_count": 24, "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", "
CloseVolume
SymbolDate
AAPL2016-10-03112.5221701800
MSFT2016-10-0357.4219189500
\n", "
" ], "text/plain": [ " Close Volume\n", "Symbol Date \n", "AAPL 2016-10-03 112.52 21701800\n", "MSFT 2016-10-03 57.42 19189500" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc[(['AAPL', 'MSFT'], '2016-10-03'), :]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Symbol Date \n", "AAPL 2016-10-03 112.52\n", "MSFT 2016-10-03 57.42\n", "Name: Close, dtype: float64" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc[(['AAPL', 'MSFT'], '2016-10-03'), 'Close']" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Symbol Date \n", "AAPL 2016-10-03 112.52\n", " 2016-10-04 113.00\n", "Name: Close, dtype: float64" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc[('AAPL', ['2016-10-03', '2016-10-04']), 'Close']" ] }, { "cell_type": "code", "execution_count": 27, "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", "
CloseVolume
SymbolDate
AAPL2016-10-03112.5221701800
2016-10-04113.0029736800
CSCO2016-10-0331.5014070500
2016-10-0431.3518460400
MSFT2016-10-0357.4219189500
2016-10-0457.2420085900
\n", "
" ], "text/plain": [ " Close Volume\n", "Symbol Date \n", "AAPL 2016-10-03 112.52 21701800\n", " 2016-10-04 113.00 29736800\n", "CSCO 2016-10-03 31.50 14070500\n", " 2016-10-04 31.35 18460400\n", "MSFT 2016-10-03 57.42 19189500\n", " 2016-10-04 57.24 20085900" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc[(slice(None), ['2016-10-03', '2016-10-04']), :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Merging DataFrames with MultiIndexes" ] }, { "cell_type": "code", "execution_count": 28, "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", "
Close
SymbolDate
AAPL2016-10-03112.52
2016-10-04113.00
2016-10-05113.05
CSCO2016-10-0331.50
2016-10-0431.35
2016-10-0531.59
MSFT2016-10-0357.42
2016-10-0457.24
2016-10-0557.64
\n", "
" ], "text/plain": [ " Close\n", "Symbol Date \n", "AAPL 2016-10-03 112.52\n", " 2016-10-04 113.00\n", " 2016-10-05 113.05\n", "CSCO 2016-10-03 31.50\n", " 2016-10-04 31.35\n", " 2016-10-05 31.59\n", "MSFT 2016-10-03 57.42\n", " 2016-10-04 57.24\n", " 2016-10-05 57.64" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "close = pd.read_csv('data/stocks.csv', usecols=[0, 1, 3], index_col=['Symbol', 'Date']).sort_index()\n", "close" ] }, { "cell_type": "code", "execution_count": 29, "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", "
Volume
SymbolDate
AAPL2016-10-0321701800
2016-10-0429736800
2016-10-0521453100
CSCO2016-10-0314070500
2016-10-0418460400
2016-10-0511808600
MSFT2016-10-0319189500
2016-10-0420085900
2016-10-0516726400
\n", "
" ], "text/plain": [ " Volume\n", "Symbol Date \n", "AAPL 2016-10-03 21701800\n", " 2016-10-04 29736800\n", " 2016-10-05 21453100\n", "CSCO 2016-10-03 14070500\n", " 2016-10-04 18460400\n", " 2016-10-05 11808600\n", "MSFT 2016-10-03 19189500\n", " 2016-10-04 20085900\n", " 2016-10-05 16726400" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "volume = pd.read_csv('data/stocks.csv', usecols=[0, 2, 3], index_col=['Symbol', 'Date']).sort_index()\n", "volume" ] }, { "cell_type": "code", "execution_count": 30, "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", "
CloseVolume
SymbolDate
AAPL2016-10-03112.5221701800
2016-10-04113.0029736800
2016-10-05113.0521453100
CSCO2016-10-0331.5014070500
2016-10-0431.3518460400
2016-10-0531.5911808600
MSFT2016-10-0357.4219189500
2016-10-0457.2420085900
2016-10-0557.6416726400
\n", "
" ], "text/plain": [ " Close Volume\n", "Symbol Date \n", "AAPL 2016-10-03 112.52 21701800\n", " 2016-10-04 113.00 29736800\n", " 2016-10-05 113.05 21453100\n", "CSCO 2016-10-03 31.50 14070500\n", " 2016-10-04 31.35 18460400\n", " 2016-10-05 31.59 11808600\n", "MSFT 2016-10-03 57.42 19189500\n", " 2016-10-04 57.24 20085900\n", " 2016-10-05 57.64 16726400" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "both = pd.merge(close, volume, left_index=True, right_index=True)\n", "both" ] }, { "cell_type": "code", "execution_count": 31, "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", "
SymbolDateCloseVolume
0AAPL2016-10-03112.5221701800
1AAPL2016-10-04113.0029736800
2AAPL2016-10-05113.0521453100
3CSCO2016-10-0331.5014070500
4CSCO2016-10-0431.3518460400
5CSCO2016-10-0531.5911808600
6MSFT2016-10-0357.4219189500
7MSFT2016-10-0457.2420085900
8MSFT2016-10-0557.6416726400
\n", "
" ], "text/plain": [ " Symbol Date Close Volume\n", "0 AAPL 2016-10-03 112.52 21701800\n", "1 AAPL 2016-10-04 113.00 29736800\n", "2 AAPL 2016-10-05 113.05 21453100\n", "3 CSCO 2016-10-03 31.50 14070500\n", "4 CSCO 2016-10-04 31.35 18460400\n", "5 CSCO 2016-10-05 31.59 11808600\n", "6 MSFT 2016-10-03 57.42 19189500\n", "7 MSFT 2016-10-04 57.24 20085900\n", "8 MSFT 2016-10-05 57.64 16726400" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "both.reset_index()" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }