{ "metadata": { "name": "", "signature": "sha256:ce0f6141b311238afce988530e0c8920ffd7854a937e1a54040717a37ecce3da" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Make New Columns Using Functions\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:** Credit: http://manishamde.github.io/blog/2013/03/07/pandas-and-python-top-10/" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Import modules\n", "import pandas as pd" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "# Example dataframe\n", "raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], \n", " 'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], \n", " 'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], \n", " 'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],\n", " 'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}\n", "df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])\n", "df" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regimentcompanynamepreTestScorepostTestScore
0 Nighthawks 1st Miller 4 25
1 Nighthawks 1st Jacobson 24 94
2 Nighthawks 2nd Ali 31 57
3 Nighthawks 2nd Milner 2 62
4 Dragoons 1st Cooze 3 70
5 Dragoons 1st Jacon 4 25
6 Dragoons 2nd Ryaner 24 94
7 Dragoons 2nd Sone 31 57
8 Scouts 1st Sloan 2 62
9 Scouts 1st Piger 3 70
10 Scouts 2nd Riani 2 62
11 Scouts 2nd Ali 3 70
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ " regiment company name preTestScore postTestScore\n", "0 Nighthawks 1st Miller 4 25\n", "1 Nighthawks 1st Jacobson 24 94\n", "2 Nighthawks 2nd Ali 31 57\n", "3 Nighthawks 2nd Milner 2 62\n", "4 Dragoons 1st Cooze 3 70\n", "5 Dragoons 1st Jacon 4 25\n", "6 Dragoons 2nd Ryaner 24 94\n", "7 Dragoons 2nd Sone 31 57\n", "8 Scouts 1st Sloan 2 62\n", "9 Scouts 1st Piger 3 70\n", "10 Scouts 2nd Riani 2 62\n", "11 Scouts 2nd Ali 3 70" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create one column as a function of two columns" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a function that takes two inputs, pre and post\n", "def pre_post_difference(pre, post):\n", " # returns the difference between post and pre\n", " return post - pre" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a variable that is the output of the function\n", "df['score_change'] = pre_post_difference(df['preTestScore'], df['postTestScore'])\n", "\n", "# View the dataframe\n", "df" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regimentcompanynamepreTestScorepostTestScorescore_change
0 Nighthawks 1st Miller 4 25 21
1 Nighthawks 1st Jacobson 24 94 70
2 Nighthawks 2nd Ali 31 57 26
3 Nighthawks 2nd Milner 2 62 60
4 Dragoons 1st Cooze 3 70 67
5 Dragoons 1st Jacon 4 25 21
6 Dragoons 2nd Ryaner 24 94 70
7 Dragoons 2nd Sone 31 57 26
8 Scouts 1st Sloan 2 62 60
9 Scouts 1st Piger 3 70 67
10 Scouts 2nd Riani 2 62 60
11 Scouts 2nd Ali 3 70 67
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ " regiment company name preTestScore postTestScore score_change\n", "0 Nighthawks 1st Miller 4 25 21\n", "1 Nighthawks 1st Jacobson 24 94 70\n", "2 Nighthawks 2nd Ali 31 57 26\n", "3 Nighthawks 2nd Milner 2 62 60\n", "4 Dragoons 1st Cooze 3 70 67\n", "5 Dragoons 1st Jacon 4 25 21\n", "6 Dragoons 2nd Ryaner 24 94 70\n", "7 Dragoons 2nd Sone 31 57 26\n", "8 Scouts 1st Sloan 2 62 60\n", "9 Scouts 1st Piger 3 70 67\n", "10 Scouts 2nd Riani 2 62 60\n", "11 Scouts 2nd Ali 3 70 67" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create two columns as a function of one column" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a function that takes one input, x\n", "def score_multipler_2x_and_3x(x):\n", " # returns two things, x multiplied by 2 and x multiplied by 3\n", " return x*2, x*3" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "# Create two new variables that take the two outputs of the function\n", "df['post_score_x2'], df['post_score_x3'] = zip(*df['postTestScore'].map(score_multipler_2x_and_3x))\n", "df" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regimentcompanynamepreTestScorepostTestScorescore_changepost_score_x2post_score_x3
0 Nighthawks 1st Miller 4 25 21 50 75
1 Nighthawks 1st Jacobson 24 94 70 188 282
2 Nighthawks 2nd Ali 31 57 26 114 171
3 Nighthawks 2nd Milner 2 62 60 124 186
4 Dragoons 1st Cooze 3 70 67 140 210
5 Dragoons 1st Jacon 4 25 21 50 75
6 Dragoons 2nd Ryaner 24 94 70 188 282
7 Dragoons 2nd Sone 31 57 26 114 171
8 Scouts 1st Sloan 2 62 60 124 186
9 Scouts 1st Piger 3 70 67 140 210
10 Scouts 2nd Riani 2 62 60 124 186
11 Scouts 2nd Ali 3 70 67 140 210
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ " regiment company name preTestScore postTestScore score_change \\\n", "0 Nighthawks 1st Miller 4 25 21 \n", "1 Nighthawks 1st Jacobson 24 94 70 \n", "2 Nighthawks 2nd Ali 31 57 26 \n", "3 Nighthawks 2nd Milner 2 62 60 \n", "4 Dragoons 1st Cooze 3 70 67 \n", "5 Dragoons 1st Jacon 4 25 21 \n", "6 Dragoons 2nd Ryaner 24 94 70 \n", "7 Dragoons 2nd Sone 31 57 26 \n", "8 Scouts 1st Sloan 2 62 60 \n", "9 Scouts 1st Piger 3 70 67 \n", "10 Scouts 2nd Riani 2 62 60 \n", "11 Scouts 2nd Ali 3 70 67 \n", "\n", " post_score_x2 post_score_x3 \n", "0 50 75 \n", "1 188 282 \n", "2 114 171 \n", "3 124 186 \n", "4 140 210 \n", "5 50 75 \n", "6 188 282 \n", "7 114 171 \n", "8 124 186 \n", "9 140 210 \n", "10 124 186 \n", "11 140 210 " ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }