Added function application and mapping snippets for Series and DataFrames.

This commit is contained in:
Donne Martin 2015-01-31 08:46:27 -05:00
parent f217da8adf
commit 936b8adee4

View File

@ -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": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" <th>c</th>\n",
" <th>d</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td> 99.451186</td>\n",
" <td> 98.867789</td>\n",
" <td> 98.676912</td>\n",
" <td> 99.999886</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td> 199.455117</td>\n",
" <td> 199.274013</td>\n",
" <td> 199.207350</td>\n",
" <td> 199.907661</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td> 299.562413</td>\n",
" <td> 298.921967</td>\n",
" <td> 298.690777</td>\n",
" <td> 299.603233</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" <th>c</th>\n",
" <th>d</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>min</th>\n",
" <td> 99.451186</td>\n",
" <td> 98.867789</td>\n",
" <td> 98.676912</td>\n",
" <td> 99.999886</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td> 299.562413</td>\n",
" <td> 298.921967</td>\n",
" <td> 298.690777</td>\n",
" <td> 299.603233</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" <th>c</th>\n",
" <th>d</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td> 99.45</td>\n",
" <td> 98.87</td>\n",
" <td> 98.68</td>\n",
" <td> 100.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td> 199.46</td>\n",
" <td> 199.27</td>\n",
" <td> 199.21</td>\n",
" <td> 199.91</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td> 299.56</td>\n",
" <td> 298.92</td>\n",
" <td> 298.69</td>\n",
" <td> 299.60</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": {}