From 936b8adee410346c5f3695437cf3fb9b86475508 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 31 Jan 2015 08:46:27 -0500 Subject: [PATCH] Added function application and mapping snippets for Series and DataFrames. --- pandas/pandas.ipynb | 296 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 294 insertions(+), 2 deletions(-) diff --git a/pandas/pandas.ipynb b/pandas/pandas.ipynb index 7dcd716..ec66649 100644 --- a/pandas/pandas.ipynb +++ b/pandas/pandas.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:1d166238da27d666eaee4041e79ad7adcc22b6b5ef5d6098ce354d0160c79ba5" + "signature": "sha256:59c1808d14cc90fbdffb75e71fc2ec13c01cc1495fab34aa19ab482445ca6801" }, "nbformat": 3, "nbformat_minor": 0, @@ -3972,7 +3972,8 @@ "cell_type": "code", "collapsed": false, "input": [ - "df_10.sub(ser_10, axis=0)" + "df_11 = df_10.sub(ser_10, axis=0)\n", + "df_11" ], "language": "python", "metadata": {}, @@ -4028,6 +4029,297 @@ } ], "prompt_number": 77 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "NumPy ufuncs (element-wise array methods) operate on pandas objects:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "df_12 = np.abs(df_11)\n", + "df_12" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "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", + "
abcd
0 99.451186 98.867789 98.676912 99.999886
1 199.455117 199.274013 199.207350 199.907661
2 299.562413 298.921967 298.690777 299.603233
\n", + "
" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 78, + "text": [ + " a b c d\n", + "0 99.451186 98.867789 98.676912 99.999886\n", + "1 199.455117 199.274013 199.207350 199.907661\n", + "2 299.562413 298.921967 298.690777 299.603233" + ] + } + ], + "prompt_number": 78 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Apply a function on 1D arrays to each column:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "func_1 = lambda x: x.max() - x.min()\n", + "df_12.apply(func_1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 79, + "text": [ + "a 200.111226\n", + "b 200.054178\n", + "c 200.013864\n", + "d 199.603347\n", + "dtype: float64" + ] + } + ], + "prompt_number": 79 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Apply a function on 1D arrays to each row:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "df_12.apply(func_1, axis=1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 80, + "text": [ + "0 1.322973\n", + "1 0.700311\n", + "2 0.912456\n", + "dtype: float64" + ] + } + ], + "prompt_number": 80 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Apply a function and return a DataFrame:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "func_2 = lambda x: Series([x.min(), x.max()], index=['min', 'max'])\n", + "df_12.apply(func_2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "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", + "
abcd
min 99.451186 98.867789 98.676912 99.999886
max 299.562413 298.921967 298.690777 299.603233
\n", + "
" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 81, + "text": [ + " a b c d\n", + "min 99.451186 98.867789 98.676912 99.999886\n", + "max 299.562413 298.921967 298.690777 299.603233" + ] + } + ], + "prompt_number": 81 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Apply an element-wise Python function to a DataFrame:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "func_3 = lambda x: '%.2f' %x\n", + "df_12.applymap(func_3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "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", + "
abcd
0 99.45 98.87 98.68 100.00
1 199.46 199.27 199.21 199.91
2 299.56 298.92 298.69 299.60
\n", + "
" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 82, + "text": [ + " a b c d\n", + "0 99.45 98.87 98.68 100.00\n", + "1 199.46 199.27 199.21 199.91\n", + "2 299.56 298.92 298.69 299.60" + ] + } + ], + "prompt_number": 82 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Apply an element-wise Python function to a Series:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "df_12['a'].map(func_3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 83, + "text": [ + "0 99.45\n", + "1 199.46\n", + "2 299.56\n", + "Name: a, dtype: object" + ] + } + ], + "prompt_number": 83 } ], "metadata": {}