{
"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",
" Date | \n",
" Close | \n",
" Volume | \n",
" Symbol | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2016-10-03 | \n",
" 31.50 | \n",
" 14070500 | \n",
" CSCO | \n",
"
\n",
" \n",
" 1 | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
" AAPL | \n",
"
\n",
" \n",
" 2 | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
" MSFT | \n",
"
\n",
" \n",
" 3 | \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
" AAPL | \n",
"
\n",
" \n",
" 4 | \n",
" 2016-10-04 | \n",
" 57.24 | \n",
" 20085900 | \n",
" MSFT | \n",
"
\n",
" \n",
" 5 | \n",
" 2016-10-04 | \n",
" 31.35 | \n",
" 18460400 | \n",
" CSCO | \n",
"
\n",
" \n",
" 6 | \n",
" 2016-10-05 | \n",
" 57.64 | \n",
" 16726400 | \n",
" MSFT | \n",
"
\n",
" \n",
" 7 | \n",
" 2016-10-05 | \n",
" 31.59 | \n",
" 11808600 | \n",
" CSCO | \n",
"
\n",
" \n",
" 8 | \n",
" 2016-10-05 | \n",
" 113.05 | \n",
" 21453100 | \n",
" AAPL | \n",
"
\n",
" \n",
"
\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",
" Date | \n",
" 2016-10-03 | \n",
" 2016-10-04 | \n",
" 2016-10-05 | \n",
"
\n",
" \n",
" Symbol | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 112.52 | \n",
" 113.00 | \n",
" 113.05 | \n",
"
\n",
" \n",
" CSCO | \n",
" 31.50 | \n",
" 31.35 | \n",
" 31.59 | \n",
"
\n",
" \n",
" MSFT | \n",
" 57.42 | \n",
" 57.24 | \n",
" 57.64 | \n",
"
\n",
" \n",
"
\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",
" Date | \n",
" 2016-10-03 | \n",
" 2016-10-04 | \n",
" 2016-10-05 | \n",
"
\n",
" \n",
" Symbol | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 112.52 | \n",
" 113.00 | \n",
" 113.05 | \n",
"
\n",
" \n",
" CSCO | \n",
" 31.50 | \n",
" 31.35 | \n",
" 31.59 | \n",
"
\n",
" \n",
" MSFT | \n",
" 57.42 | \n",
" 57.24 | \n",
" 57.64 | \n",
"
\n",
" \n",
"
\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",
" Date | \n",
" 2016-10-03 | \n",
" 2016-10-04 | \n",
" 2016-10-05 | \n",
"
\n",
" \n",
" Symbol | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 112.52 | \n",
" 113.00 | \n",
" 113.05 | \n",
"
\n",
" \n",
" CSCO | \n",
" 31.50 | \n",
" 31.35 | \n",
" 31.59 | \n",
"
\n",
" \n",
" MSFT | \n",
" 57.42 | \n",
" 57.24 | \n",
" 57.64 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" CSCO | \n",
" 2016-10-03 | \n",
" 31.50 | \n",
" 14070500 | \n",
"
\n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
"
\n",
" \n",
" AAPL | \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-04 | \n",
" 57.24 | \n",
" 20085900 | \n",
"
\n",
" \n",
" CSCO | \n",
" 2016-10-04 | \n",
" 31.35 | \n",
" 18460400 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-05 | \n",
" 57.64 | \n",
" 16726400 | \n",
"
\n",
" \n",
" CSCO | \n",
" 2016-10-05 | \n",
" 31.59 | \n",
" 11808600 | \n",
"
\n",
" \n",
" AAPL | \n",
" 2016-10-05 | \n",
" 113.05 | \n",
" 21453100 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 113.05 | \n",
" 21453100 | \n",
"
\n",
" \n",
" CSCO | \n",
" 2016-10-03 | \n",
" 31.50 | \n",
" 14070500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 31.35 | \n",
" 18460400 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 31.59 | \n",
" 11808600 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 57.24 | \n",
" 20085900 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 57.64 | \n",
" 16726400 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 113.05 | \n",
" 21453100 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 113.05 | \n",
" 21453100 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 57.24 | \n",
" 20085900 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 57.64 | \n",
" 16726400 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
"
\n",
" \n",
" CSCO | \n",
" 2016-10-03 | \n",
" 31.50 | \n",
" 14070500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 31.35 | \n",
" 18460400 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 57.24 | \n",
" 20085900 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 113.00 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 113.05 | \n",
"
\n",
" \n",
" CSCO | \n",
" 2016-10-03 | \n",
" 31.50 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 31.35 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 31.59 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 57.24 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 57.64 | \n",
"
\n",
" \n",
"
\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",
" Volume | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 21701800 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 29736800 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 21453100 | \n",
"
\n",
" \n",
" CSCO | \n",
" 2016-10-03 | \n",
" 14070500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 18460400 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 11808600 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 19189500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 20085900 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 16726400 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Symbol | \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 113.05 | \n",
" 21453100 | \n",
"
\n",
" \n",
" CSCO | \n",
" 2016-10-03 | \n",
" 31.50 | \n",
" 14070500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 31.35 | \n",
" 18460400 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 31.59 | \n",
" 11808600 | \n",
"
\n",
" \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
"
\n",
" \n",
" 2016-10-04 | \n",
" 57.24 | \n",
" 20085900 | \n",
"
\n",
" \n",
" 2016-10-05 | \n",
" 57.64 | \n",
" 16726400 | \n",
"
\n",
" \n",
"
\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",
" Symbol | \n",
" Date | \n",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AAPL | \n",
" 2016-10-03 | \n",
" 112.52 | \n",
" 21701800 | \n",
"
\n",
" \n",
" 1 | \n",
" AAPL | \n",
" 2016-10-04 | \n",
" 113.00 | \n",
" 29736800 | \n",
"
\n",
" \n",
" 2 | \n",
" AAPL | \n",
" 2016-10-05 | \n",
" 113.05 | \n",
" 21453100 | \n",
"
\n",
" \n",
" 3 | \n",
" CSCO | \n",
" 2016-10-03 | \n",
" 31.50 | \n",
" 14070500 | \n",
"
\n",
" \n",
" 4 | \n",
" CSCO | \n",
" 2016-10-04 | \n",
" 31.35 | \n",
" 18460400 | \n",
"
\n",
" \n",
" 5 | \n",
" CSCO | \n",
" 2016-10-05 | \n",
" 31.59 | \n",
" 11808600 | \n",
"
\n",
" \n",
" 6 | \n",
" MSFT | \n",
" 2016-10-03 | \n",
" 57.42 | \n",
" 19189500 | \n",
"
\n",
" \n",
" 7 | \n",
" MSFT | \n",
" 2016-10-04 | \n",
" 57.24 | \n",
" 20085900 | \n",
"
\n",
" \n",
" 8 | \n",
" MSFT | \n",
" 2016-10-05 | \n",
" 57.64 | \n",
" 16726400 | \n",
"
\n",
" \n",
"
\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
}