{ "cells": [ { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a DataFrame from a list" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fund_data = [['2016-04-01', 121.55], ['2016-03-01', 115.76], ['2016-02-01', 110.25], ['2016-01-01', 100]]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fund_data_frame = pd.DataFrame(fund_data, columns=['Date', 'Value'])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "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", "
DateValue
02016-04-01121.55
12016-03-01115.76
22016-02-01110.25
32016-01-01100.00
\n", "
" ], "text/plain": [ " Date Value\n", "0 2016-04-01 121.55\n", "1 2016-03-01 115.76\n", "2 2016-02-01 110.25\n", "3 2016-01-01 100.00" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding and removing columns" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 0.05\n", "1 0.05\n", "2 0.05\n", "3 0.05\n", "dtype: float64" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "returns = pd.Series([0.05] * 4)\n", "returns" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fund_data_frame['Return'] = returns" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-04-01121.550.05
12016-03-01115.760.05
22016-02-01110.250.05
32016-01-01100.000.05
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-04-01 121.55 0.05\n", "1 2016-03-01 115.76 0.05\n", "2 2016-02-01 110.25 0.05\n", "3 2016-01-01 100.00 0.05" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame.head()" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "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", "
DateReturn
02016-04-010.05
12016-03-010.05
22016-02-010.05
32016-01-010.05
\n", "
" ], "text/plain": [ " Date Return\n", "0 2016-04-01 0.05\n", "1 2016-03-01 0.05\n", "2 2016-02-01 0.05\n", "3 2016-01-01 0.05" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Note that this is not an in-place change - just a returned copy\n", "fund_data_frame.drop('Value', axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Doing math on a column" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-04-01121.550.1
12016-03-01115.760.1
22016-02-01110.250.1
32016-01-01100.000.1
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-04-01 121.55 0.1\n", "1 2016-03-01 115.76 0.1\n", "2 2016-02-01 110.25 0.1\n", "3 2016-01-01 100.00 0.1" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame['Return'] = fund_data_frame['Return'] + pd.Series([0.05]*4)\n", "fund_data_frame" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-04-01121.550.05
12016-03-01115.760.05
22016-02-01110.250.05
32016-01-01100.000.05
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-04-01 121.55 0.05\n", "1 2016-03-01 115.76 0.05\n", "2 2016-02-01 110.25 0.05\n", "3 2016-01-01 100.00 0.05" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame['Return'] = fund_data_frame['Return'] / 2\n", "fund_data_frame" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reversing a DataFrame\n", "\n", "And let's say we want the row indicies to not be reversed." ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-01-01100.000.05
12016-02-01110.250.05
22016-03-01115.760.05
32016-04-01121.550.05
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-01-01 100.00 0.05\n", "1 2016-02-01 110.25 0.05\n", "2 2016-03-01 115.76 0.05\n", "3 2016-04-01 121.55 0.05" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame = fund_data_frame[::-1]\n", "fund_data_frame.index = range(len(fund_data_frame))\n", "fund_data_frame" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtering" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "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", "
DateValueReturn
22016-03-01115.760.05
32016-04-01121.550.05
\n", "
" ], "text/plain": [ " Date Value Return\n", "2 2016-03-01 115.76 0.05\n", "3 2016-04-01 121.55 0.05" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame[fund_data_frame.Value > 111]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting by index" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-01-01100.000.05
32016-04-01121.550.05
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-01-01 100.00 0.05\n", "3 2016-04-01 121.55 0.05" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame.ix[[0,3]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding and removing rows" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-01-01100.000.050
12016-02-01110.250.050
22016-03-01115.760.050
32016-04-01121.550.050
42016-05-01130.000.695
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-01-01 100.00 0.050\n", "1 2016-02-01 110.25 0.050\n", "2 2016-03-01 115.76 0.050\n", "3 2016-04-01 121.55 0.050\n", "4 2016-05-01 130.00 0.695" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fund_data_frame = fund_data_frame.append({'Date': '2016-05-01', 'Value': 130, 'Return': 0.695}, ignore_index=True)\n", "fund_data_frame" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-01-01100.000.05
12016-02-01110.250.05
22016-03-01115.760.05
32016-04-01121.550.05
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-01-01 100.00 0.05\n", "1 2016-02-01 110.25 0.05\n", "2 2016-03-01 115.76 0.05\n", "3 2016-04-01 121.55 0.05" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Notice that this is a returned copy, not an in-place change\n", "fund_data_frame[fund_data_frame.Value != 130]" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "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", "
DateValueReturn
02016-01-01100.000.05
12016-02-01110.250.05
22016-03-01115.760.05
32016-04-01121.550.05
\n", "
" ], "text/plain": [ " Date Value Return\n", "0 2016-01-01 100.00 0.05\n", "1 2016-02-01 110.25 0.05\n", "2 2016-03-01 115.76 0.05\n", "3 2016-04-01 121.55 0.05" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Alternatively, we can slice\n", "fund_data_frame.ix[0:3]" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }