{
"metadata": {
"name": "",
"signature": "sha256:c8ab1a3c99e7c72951c91e74991b8837884cd9e3863f1cd1833651e180ff32bd"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Back to the GitHub repository](https://github.com/rasbt/python_reference)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext watermark\n",
"%watermark -a 'Sebastian Raschka' -v -d -p pandas"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sebastian Raschka 28/01/2015 \n",
"\n",
"CPython 3.4.2\n",
"IPython 2.3.1\n",
"\n",
"pandas 0.15.2\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[More information](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) about the `watermark` magic command extension."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Things in Pandas I Wish I'd Had Known Earlier"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is just a small but growing collection of pandas snippets that I find occasionally and particularly useful -- consider it as my personal notebook. Suggestions, tips, and contributions are very, very welcome!"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Sections"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [Loading Some Example Data](#Loading-Some-Example-Data)\n",
"- [Renaming Columns](#Renaming-Columns)\n",
" - [Converting Column Names to Lowercase](#Converting-Column-Names-to-Lowercase)\n",
" - [Renaming Particular Columns](#Renaming-Particular-Columns)\n",
"- [Applying Computations Rows-wise](#Applying-Computations-Rows-wise)\n",
" - [Changing Values in a Column](#Changing-Values-in-a-Column)\n",
" - [Adding a New Column](#Adding-a-New-Column)\n",
" - [Applying Functions to Multiple Columns](#Applying-Functions-to-Multiple-Columns)\n",
"- [Missing Values aka NaNs](#Missing-Values-aka-NaNs)\n",
" - [Counting Rows with NaNs](#Counting-Rows-with-NaNs)\n",
" - [Selecting NaN Rows](#Selecting-NaN-Rows)\n",
" - [Selecting non-NaN Rows](#Selecting-non-NaN-Rows)\n",
" - [Filling NaN Rows](#Filling-NaN-Rows)\n",
"- [Appending Rows to a DataFrame](#Appending-Rows-to-a-DataFrame)\n",
"- [Sorting and Reindexing DataFrames](#Sorting-and-Reindexing-DataFrames)\n",
"- [Updating Columns](#Updating-Columns)\n",
"- [Chaining Conditions - Using Bitwise Operators](#Chaining-Conditions---Using-Bitwise-Operators)\n",
"- [Column Types](#Column-Types)\n",
" - [Printing Column Types](#Printing-Column-Types)\n",
" - [Selecting by Column Type](#Selecting-by-Column-Type)\n",
" - [Converting Column Types](#Converting-Column-Types)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Loading Some Example Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I am heavily into sports prediction (via a machine learning approach) these days. So, let us use a (very) small subset of the soccer data that I am just working with."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv('https://raw.githubusercontent.com/rasbt/python_reference/master/Data/some_soccer_data.csv')\n",
"df"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" PLAYER | \n",
" SALARY | \n",
" GP | \n",
" G | \n",
" A | \n",
" SOT | \n",
" PPG | \n",
" P | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Sergio Ag\u00fcero\\n Forward \u2014 Manchester City | \n",
" $19.2m | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
"
\n",
" \n",
" 1 | \n",
" Eden Hazard\\n Midfield \u2014 Chelsea | \n",
" $18.9m | \n",
" 21 | \n",
" 8 | \n",
" 4 | \n",
" 17 | \n",
" 13.05 | \n",
" 274.04 | \n",
"
\n",
" \n",
" 2 | \n",
" Alexis S\u00e1nchez\\n Forward \u2014 Arsenal | \n",
" $17.6m | \n",
" NaN | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
"
\n",
" \n",
" 3 | \n",
" Yaya Tour\u00e9\\n Midfield \u2014 Manchester City | \n",
" $16.6m | \n",
" 18 | \n",
" 7 | \n",
" 1 | \n",
" 19 | \n",
" 10.99 | \n",
" 197.91 | \n",
"
\n",
" \n",
" 4 | \n",
" \u00c1ngel Di Mar\u00eda\\n Midfield \u2014 Manchester United | \n",
" $15.0m | \n",
" 13 | \n",
" 3 | \n",
" NaN | \n",
" 13 | \n",
" 10.17 | \n",
" 132.23 | \n",
"
\n",
" \n",
" 5 | \n",
" Santiago Cazorla\\n Midfield \u2014 Arsenal | \n",
" $14.8m | \n",
" 20 | \n",
" 4 | \n",
" NaN | \n",
" 20 | \n",
" 9.97 | \n",
" NaN | \n",
"
\n",
" \n",
" 6 | \n",
" David Silva\\n Midfield \u2014 Manchester City | \n",
" $14.3m | \n",
" 15 | \n",
" 6 | \n",
" 2 | \n",
" 11 | \n",
" 10.35 | \n",
" 155.26 | \n",
"
\n",
" \n",
" 7 | \n",
" Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea | \n",
" $14.0m | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
"
\n",
" \n",
" 8 | \n",
" Saido Berahino\\n Forward \u2014 West Brom | \n",
" $13.8m | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
"
\n",
" \n",
" 9 | \n",
" Steven Gerrard\\n Midfield \u2014 Liverpool | \n",
" $13.8m | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
" PLAYER SALARY GP G A SOT \\\n",
"0 Sergio Ag\u00fcero\\n Forward \u2014 Manchester City $19.2m 16 14 3 34 \n",
"1 Eden Hazard\\n Midfield \u2014 Chelsea $18.9m 21 8 4 17 \n",
"2 Alexis S\u00e1nchez\\n Forward \u2014 Arsenal $17.6m NaN 12 7 29 \n",
"3 Yaya Tour\u00e9\\n Midfield \u2014 Manchester City $16.6m 18 7 1 19 \n",
"4 \u00c1ngel Di Mar\u00eda\\n Midfield \u2014 Manchester United $15.0m 13 3 NaN 13 \n",
"5 Santiago Cazorla\\n Midfield \u2014 Arsenal $14.8m 20 4 NaN 20 \n",
"6 David Silva\\n Midfield \u2014 Manchester City $14.3m 15 6 2 11 \n",
"7 Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea $14.0m 20 2 14 10 \n",
"8 Saido Berahino\\n Forward \u2014 West Brom $13.8m 21 9 0 20 \n",
"9 Steven Gerrard\\n Midfield \u2014 Liverpool $13.8m 20 5 1 11 \n",
"\n",
" PPG P \n",
"0 13.12 209.98 \n",
"1 13.05 274.04 \n",
"2 11.19 223.86 \n",
"3 10.99 197.91 \n",
"4 10.17 132.23 \n",
"5 9.97 NaN \n",
"6 10.35 155.26 \n",
"7 10.47 209.49 \n",
"8 7.02 147.43 \n",
"9 7.50 150.01 "
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Renaming Columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Converting Column Names to Lowercase"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Converting column names to lowercase\n",
"\n",
"df.columns = [c.lower() for c in df.columns]\n",
"\n",
"# or\n",
"# df.rename(columns=lambda x : x.lower())\n",
"\n",
"df.tail(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" gp | \n",
" g | \n",
" a | \n",
" sot | \n",
" ppg | \n",
" p | \n",
"
\n",
" \n",
" \n",
" \n",
" 7 | \n",
" Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea | \n",
" $14.0m | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
"
\n",
" \n",
" 8 | \n",
" Saido Berahino\\n Forward \u2014 West Brom | \n",
" $13.8m | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
"
\n",
" \n",
" 9 | \n",
" Steven Gerrard\\n Midfield \u2014 Liverpool | \n",
" $13.8m | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
" player salary gp g a sot ppg \\\n",
"7 Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea $14.0m 20 2 14 10 10.47 \n",
"8 Saido Berahino\\n Forward \u2014 West Brom $13.8m 21 9 0 20 7.02 \n",
"9 Steven Gerrard\\n Midfield \u2014 Liverpool $13.8m 20 5 1 11 7.50 \n",
"\n",
" p \n",
"7 209.49 \n",
"8 147.43 \n",
"9 150.01 "
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Renaming Particular Columns"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df = df.rename(columns={'p': 'points', \n",
" 'gp': 'games',\n",
" 'sot': 'shots_on_target',\n",
" 'g': 'goals',\n",
" 'ppg': 'points_per_game',\n",
" 'a': 'assists',})\n",
"\n",
"df.tail(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
"
\n",
" \n",
" \n",
" \n",
" 7 | \n",
" Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea | \n",
" $14.0m | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
"
\n",
" \n",
" 8 | \n",
" Saido Berahino\\n Forward \u2014 West Brom | \n",
" $13.8m | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
"
\n",
" \n",
" 9 | \n",
" Steven Gerrard\\n Midfield \u2014 Liverpool | \n",
" $13.8m | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
" player salary games goals assists \\\n",
"7 Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea $14.0m 20 2 14 \n",
"8 Saido Berahino\\n Forward \u2014 West Brom $13.8m 21 9 0 \n",
"9 Steven Gerrard\\n Midfield \u2014 Liverpool $13.8m 20 5 1 \n",
"\n",
" shots_on_target points_per_game points \n",
"7 10 10.47 209.49 \n",
"8 20 7.02 147.43 \n",
"9 11 7.50 150.01 "
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Applying Computations Rows-wise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Changing Values in a Column"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Processing `salary` column\n",
"\n",
"df['salary'] = df['salary'].apply(lambda x: x.strip('$m'))\n",
"df.tail()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
"
\n",
" \n",
" \n",
" \n",
" 5 | \n",
" Santiago Cazorla\\n Midfield \u2014 Arsenal | \n",
" 14.8 | \n",
" 20 | \n",
" 4 | \n",
" NaN | \n",
" 20 | \n",
" 9.97 | \n",
" NaN | \n",
"
\n",
" \n",
" 6 | \n",
" David Silva\\n Midfield \u2014 Manchester City | \n",
" 14.3 | \n",
" 15 | \n",
" 6 | \n",
" 2 | \n",
" 11 | \n",
" 10.35 | \n",
" 155.26 | \n",
"
\n",
" \n",
" 7 | \n",
" Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea | \n",
" 14.0 | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
"
\n",
" \n",
" 8 | \n",
" Saido Berahino\\n Forward \u2014 West Brom | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
"
\n",
" \n",
" 9 | \n",
" Steven Gerrard\\n Midfield \u2014 Liverpool | \n",
" 13.8 | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
" player salary games goals assists \\\n",
"5 Santiago Cazorla\\n Midfield \u2014 Arsenal 14.8 20 4 NaN \n",
"6 David Silva\\n Midfield \u2014 Manchester City 14.3 15 6 2 \n",
"7 Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea 14.0 20 2 14 \n",
"8 Saido Berahino\\n Forward \u2014 West Brom 13.8 21 9 0 \n",
"9 Steven Gerrard\\n Midfield \u2014 Liverpool 13.8 20 5 1 \n",
"\n",
" shots_on_target points_per_game points \n",
"5 20 9.97 NaN \n",
"6 11 10.35 155.26 \n",
"7 10 10.47 209.49 \n",
"8 20 7.02 147.43 \n",
"9 11 7.50 150.01 "
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Adding a New Column"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df['team'] = pd.Series('', index=df.index)\n",
"\n",
"# or\n",
"df.insert(loc=8, column='position', value='') \n",
"\n",
"df.tail(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 7 | \n",
" Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea | \n",
" 14.0 | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
" | \n",
" | \n",
"
\n",
" \n",
" 8 | \n",
" Saido Berahino\\n Forward \u2014 West Brom | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" | \n",
" | \n",
"
\n",
" \n",
" 9 | \n",
" Steven Gerrard\\n Midfield \u2014 Liverpool | \n",
" 13.8 | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
" | \n",
" | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
" player salary games goals assists \\\n",
"7 Cesc F\u00e0bregas\\n Midfield \u2014 Chelsea 14.0 20 2 14 \n",
"8 Saido Berahino\\n Forward \u2014 West Brom 13.8 21 9 0 \n",
"9 Steven Gerrard\\n Midfield \u2014 Liverpool 13.8 20 5 1 \n",
"\n",
" shots_on_target points_per_game points position team \n",
"7 10 10.47 209.49 \n",
"8 20 7.02 147.43 \n",
"9 11 7.50 150.01 "
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Processing `player` column\n",
"\n",
"def process_player_col(text):\n",
" name, rest = text.split('\\n')\n",
" position, team = [x.strip() for x in rest.split(' \u2014 ')]\n",
" return pd.Series([name, team, position])\n",
"\n",
"df[['player', 'team', 'position']] = df.player.apply(process_player_col)\n",
"\n",
"# modified after tip from reddit.com/user/hharison\n",
"#\n",
"# Alternative (inferior) approach:\n",
"#\n",
"#for idx,row in df.iterrows():\n",
"# name, position, team = process_player_col(row['player'])\n",
"# df.ix[idx, 'player'], df.ix[idx, 'position'], df.ix[idx, 'team'] = name, position, team\n",
" \n",
"df.tail(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 7 | \n",
" Cesc F\u00e0bregas | \n",
" 14.0 | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
" Midfield | \n",
" Chelsea | \n",
"
\n",
" \n",
" 8 | \n",
" Saido Berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" Forward | \n",
" West Brom | \n",
"
\n",
" \n",
" 9 | \n",
" Steven Gerrard | \n",
" 13.8 | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
" Midfield | \n",
" Liverpool | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
" player salary games goals assists shots_on_target \\\n",
"7 Cesc F\u00e0bregas 14.0 20 2 14 10 \n",
"8 Saido Berahino 13.8 21 9 0 20 \n",
"9 Steven Gerrard 13.8 20 5 1 11 \n",
"\n",
" points_per_game points position team \n",
"7 10.47 209.49 Midfield Chelsea \n",
"8 7.02 147.43 Forward West Brom \n",
"9 7.50 150.01 Midfield Liverpool "
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Applying Functions to Multiple Columns"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cols = ['player', 'position', 'team']\n",
"df[cols] = df[cols].applymap(lambda x: x.lower())\n",
"df.head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" sergio ag\u00fcero | \n",
" 19.2 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 1 | \n",
" eden hazard | \n",
" 18.9 | \n",
" 21 | \n",
" 8 | \n",
" 4 | \n",
" 17 | \n",
" 13.05 | \n",
" 274.04 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 2 | \n",
" alexis s\u00e1nchez | \n",
" 17.6 | \n",
" NaN | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 3 | \n",
" yaya tour\u00e9 | \n",
" 16.6 | \n",
" 18 | \n",
" 7 | \n",
" 1 | \n",
" 19 | \n",
" 10.99 | \n",
" 197.91 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
" 4 | \n",
" \u00e1ngel di mar\u00eda | \n",
" 15.0 | \n",
" 13 | \n",
" 3 | \n",
" NaN | \n",
" 13 | \n",
" 10.17 | \n",
" 132.23 | \n",
" midfield | \n",
" manchester united | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
" player salary games goals assists shots_on_target \\\n",
"0 sergio ag\u00fcero 19.2 16 14 3 34 \n",
"1 eden hazard 18.9 21 8 4 17 \n",
"2 alexis s\u00e1nchez 17.6 NaN 12 7 29 \n",
"3 yaya tour\u00e9 16.6 18 7 1 19 \n",
"4 \u00e1ngel di mar\u00eda 15.0 13 3 NaN 13 \n",
"\n",
" points_per_game points position team \n",
"0 13.12 209.98 forward manchester city \n",
"1 13.05 274.04 midfield chelsea \n",
"2 11.19 223.86 forward arsenal \n",
"3 10.99 197.91 midfield manchester city \n",
"4 10.17 132.23 midfield manchester united "
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Missing Values aka NaNs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Counting Rows with NaNs"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nans = df.shape[0] - df.dropna().shape[0]\n",
"\n",
"print('%d rows have missing values' % nans)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"3 rows have missing values\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Selecting NaN Rows"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Selecting all rows that have NaNs in the `assists` column\n",
"\n",
"df[df['assists'].isnull()]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 4 | \n",
" \u00e1ngel di mar\u00eda | \n",
" 15.0 | \n",
" 13 | \n",
" 3 | \n",
" NaN | \n",
" 13 | \n",
" 10.17 | \n",
" 132.23 | \n",
" midfield | \n",
" manchester united | \n",
"
\n",
" \n",
" 5 | \n",
" santiago cazorla | \n",
" 14.8 | \n",
" 20 | \n",
" 4 | \n",
" NaN | \n",
" 20 | \n",
" 9.97 | \n",
" NaN | \n",
" midfield | \n",
" arsenal | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
" player salary games goals assists shots_on_target \\\n",
"4 \u00e1ngel di mar\u00eda 15.0 13 3 NaN 13 \n",
"5 santiago cazorla 14.8 20 4 NaN 20 \n",
"\n",
" points_per_game points position team \n",
"4 10.17 132.23 midfield manchester united \n",
"5 9.97 NaN midfield arsenal "
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Selecting non-NaN Rows"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df[df['assists'].notnull()]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" sergio ag\u00fcero | \n",
" 19.2 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 1 | \n",
" eden hazard | \n",
" 18.9 | \n",
" 21 | \n",
" 8 | \n",
" 4 | \n",
" 17 | \n",
" 13.05 | \n",
" 274.04 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 2 | \n",
" alexis s\u00e1nchez | \n",
" 17.6 | \n",
" NaN | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 3 | \n",
" yaya tour\u00e9 | \n",
" 16.6 | \n",
" 18 | \n",
" 7 | \n",
" 1 | \n",
" 19 | \n",
" 10.99 | \n",
" 197.91 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
" 6 | \n",
" david silva | \n",
" 14.3 | \n",
" 15 | \n",
" 6 | \n",
" 2 | \n",
" 11 | \n",
" 10.35 | \n",
" 155.26 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
" 7 | \n",
" cesc f\u00e0bregas | \n",
" 14.0 | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 8 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
" 9 | \n",
" steven gerrard | \n",
" 13.8 | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
" midfield | \n",
" liverpool | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
" player salary games goals assists shots_on_target \\\n",
"0 sergio ag\u00fcero 19.2 16 14 3 34 \n",
"1 eden hazard 18.9 21 8 4 17 \n",
"2 alexis s\u00e1nchez 17.6 NaN 12 7 29 \n",
"3 yaya tour\u00e9 16.6 18 7 1 19 \n",
"6 david silva 14.3 15 6 2 11 \n",
"7 cesc f\u00e0bregas 14.0 20 2 14 10 \n",
"8 saido berahino 13.8 21 9 0 20 \n",
"9 steven gerrard 13.8 20 5 1 11 \n",
"\n",
" points_per_game points position team \n",
"0 13.12 209.98 forward manchester city \n",
"1 13.05 274.04 midfield chelsea \n",
"2 11.19 223.86 forward arsenal \n",
"3 10.99 197.91 midfield manchester city \n",
"6 10.35 155.26 midfield manchester city \n",
"7 10.47 209.49 midfield chelsea \n",
"8 7.02 147.43 forward west brom \n",
"9 7.50 150.01 midfield liverpool "
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Filling NaN Rows"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Filling NaN cells with default value 0\n",
"\n",
"df.fillna(value=0, inplace=True)\n",
"df"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" sergio ag\u00fcero | \n",
" 19.2 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 1 | \n",
" eden hazard | \n",
" 18.9 | \n",
" 21 | \n",
" 8 | \n",
" 4 | \n",
" 17 | \n",
" 13.05 | \n",
" 274.04 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 2 | \n",
" alexis s\u00e1nchez | \n",
" 17.6 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 3 | \n",
" yaya tour\u00e9 | \n",
" 16.6 | \n",
" 18 | \n",
" 7 | \n",
" 1 | \n",
" 19 | \n",
" 10.99 | \n",
" 197.91 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
" 4 | \n",
" \u00e1ngel di mar\u00eda | \n",
" 15.0 | \n",
" 13 | \n",
" 3 | \n",
" 0 | \n",
" 13 | \n",
" 10.17 | \n",
" 132.23 | \n",
" midfield | \n",
" manchester united | \n",
"
\n",
" \n",
" 5 | \n",
" santiago cazorla | \n",
" 14.8 | \n",
" 20 | \n",
" 4 | \n",
" 0 | \n",
" 20 | \n",
" 9.97 | \n",
" 0.00 | \n",
" midfield | \n",
" arsenal | \n",
"
\n",
" \n",
" 6 | \n",
" david silva | \n",
" 14.3 | \n",
" 15 | \n",
" 6 | \n",
" 2 | \n",
" 11 | \n",
" 10.35 | \n",
" 155.26 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
" 7 | \n",
" cesc f\u00e0bregas | \n",
" 14.0 | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 8 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
" 9 | \n",
" steven gerrard | \n",
" 13.8 | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
" midfield | \n",
" liverpool | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
" player salary games goals assists shots_on_target \\\n",
"0 sergio ag\u00fcero 19.2 16 14 3 34 \n",
"1 eden hazard 18.9 21 8 4 17 \n",
"2 alexis s\u00e1nchez 17.6 0 12 7 29 \n",
"3 yaya tour\u00e9 16.6 18 7 1 19 \n",
"4 \u00e1ngel di mar\u00eda 15.0 13 3 0 13 \n",
"5 santiago cazorla 14.8 20 4 0 20 \n",
"6 david silva 14.3 15 6 2 11 \n",
"7 cesc f\u00e0bregas 14.0 20 2 14 10 \n",
"8 saido berahino 13.8 21 9 0 20 \n",
"9 steven gerrard 13.8 20 5 1 11 \n",
"\n",
" points_per_game points position team \n",
"0 13.12 209.98 forward manchester city \n",
"1 13.05 274.04 midfield chelsea \n",
"2 11.19 223.86 forward arsenal \n",
"3 10.99 197.91 midfield manchester city \n",
"4 10.17 132.23 midfield manchester united \n",
"5 9.97 0.00 midfield arsenal \n",
"6 10.35 155.26 midfield manchester city \n",
"7 10.47 209.49 midfield chelsea \n",
"8 7.02 147.43 forward west brom \n",
"9 7.50 150.01 midfield liverpool "
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Appending Rows to a DataFrame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Adding an \"empty\" row to the DataFrame\n",
"\n",
"import numpy as np\n",
"\n",
"df = df.append(pd.Series(\n",
" [np.nan]*len(df.columns), # Fill cells with NaNs\n",
" index=df.columns), \n",
" ignore_index=True)\n",
"\n",
"df.tail(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 8 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
" 9 | \n",
" steven gerrard | \n",
" 13.8 | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
" midfield | \n",
" liverpool | \n",
"
\n",
" \n",
" 10 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
" player salary games goals assists shots_on_target \\\n",
"8 saido berahino 13.8 21 9 0 20 \n",
"9 steven gerrard 13.8 20 5 1 11 \n",
"10 NaN NaN NaN NaN NaN NaN \n",
"\n",
" points_per_game points position team \n",
"8 7.02 147.43 forward west brom \n",
"9 7.50 150.01 midfield liverpool \n",
"10 NaN NaN NaN NaN "
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Filling cells with data\n",
"\n",
"df.loc[df.index[-1], 'player'] = 'new player'\n",
"df.loc[df.index[-1], 'salary'] = 12.3\n",
"df.tail(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 8 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
" 9 | \n",
" steven gerrard | \n",
" 13.8 | \n",
" 20 | \n",
" 5 | \n",
" 1 | \n",
" 11 | \n",
" 7.50 | \n",
" 150.01 | \n",
" midfield | \n",
" liverpool | \n",
"
\n",
" \n",
" 10 | \n",
" new player | \n",
" 12.3 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
" player salary games goals assists shots_on_target \\\n",
"8 saido berahino 13.8 21 9 0 20 \n",
"9 steven gerrard 13.8 20 5 1 11 \n",
"10 new player 12.3 NaN NaN NaN NaN \n",
"\n",
" points_per_game points position team \n",
"8 7.02 147.43 forward west brom \n",
"9 7.50 150.01 midfield liverpool \n",
"10 NaN NaN NaN NaN "
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Sorting and Reindexing DataFrames"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Sorting the DataFrame by a certain column (from highest to lowest)\n",
"\n",
"df.sort('goals', ascending=False, inplace=True)\n",
"df.head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" sergio ag\u00fcero | \n",
" 19.2 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 2 | \n",
" alexis s\u00e1nchez | \n",
" 17.6 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 8 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
" 1 | \n",
" eden hazard | \n",
" 18.9 | \n",
" 21 | \n",
" 8 | \n",
" 4 | \n",
" 17 | \n",
" 13.05 | \n",
" 274.04 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 3 | \n",
" yaya tour\u00e9 | \n",
" 16.6 | \n",
" 18 | \n",
" 7 | \n",
" 1 | \n",
" 19 | \n",
" 10.99 | \n",
" 197.91 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"text": [
" player salary games goals assists shots_on_target \\\n",
"0 sergio ag\u00fcero 19.2 16 14 3 34 \n",
"2 alexis s\u00e1nchez 17.6 0 12 7 29 \n",
"8 saido berahino 13.8 21 9 0 20 \n",
"1 eden hazard 18.9 21 8 4 17 \n",
"3 yaya tour\u00e9 16.6 18 7 1 19 \n",
"\n",
" points_per_game points position team \n",
"0 13.12 209.98 forward manchester city \n",
"2 11.19 223.86 forward arsenal \n",
"8 7.02 147.43 forward west brom \n",
"1 13.05 274.04 midfield chelsea \n",
"3 10.99 197.91 midfield manchester city "
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Optional reindexing of the DataFrame after sorting\n",
"\n",
"df.index = range(1,len(df.index)+1)\n",
"df.head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" sergio ag\u00fcero | \n",
" 19.2 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 2 | \n",
" alexis s\u00e1nchez | \n",
" 17.6 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 3 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
" 4 | \n",
" eden hazard | \n",
" 18.9 | \n",
" 21 | \n",
" 8 | \n",
" 4 | \n",
" 17 | \n",
" 13.05 | \n",
" 274.04 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 5 | \n",
" yaya tour\u00e9 | \n",
" 16.6 | \n",
" 18 | \n",
" 7 | \n",
" 1 | \n",
" 19 | \n",
" 10.99 | \n",
" 197.91 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
" player salary games goals assists shots_on_target \\\n",
"1 sergio ag\u00fcero 19.2 16 14 3 34 \n",
"2 alexis s\u00e1nchez 17.6 0 12 7 29 \n",
"3 saido berahino 13.8 21 9 0 20 \n",
"4 eden hazard 18.9 21 8 4 17 \n",
"5 yaya tour\u00e9 16.6 18 7 1 19 \n",
"\n",
" points_per_game points position team \n",
"1 13.12 209.98 forward manchester city \n",
"2 11.19 223.86 forward arsenal \n",
"3 7.02 147.43 forward west brom \n",
"4 13.05 274.04 midfield chelsea \n",
"5 10.99 197.91 midfield manchester city "
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Updating Columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Creating a dummy DataFrame with changes in the `salary` column\n",
"\n",
"df_2 = df.copy()\n",
"df_2.loc[0:2, 'salary'] = [20.0, 15.0]\n",
"df_2.head(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" sergio ag\u00fcero | \n",
" 20 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 2 | \n",
" alexis s\u00e1nchez | \n",
" 15 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 3 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": [
" player salary games goals assists shots_on_target \\\n",
"1 sergio ag\u00fcero 20 16 14 3 34 \n",
"2 alexis s\u00e1nchez 15 0 12 7 29 \n",
"3 saido berahino 13.8 21 9 0 20 \n",
"\n",
" points_per_game points position team \n",
"1 13.12 209.98 forward manchester city \n",
"2 11.19 223.86 forward arsenal \n",
"3 7.02 147.43 forward west brom "
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Temporarily use the `player` columns as indices to \n",
"# apply the update functions\n",
"\n",
"df.set_index('player', inplace=True)\n",
"df_2.set_index('player', inplace=True)\n",
"df.head(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" player | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" sergio ag\u00fcero | \n",
" 19.2 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" alexis s\u00e1nchez | \n",
" 17.6 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
" salary games goals assists shots_on_target \\\n",
"player \n",
"sergio ag\u00fcero 19.2 16 14 3 34 \n",
"alexis s\u00e1nchez 17.6 0 12 7 29 \n",
"saido berahino 13.8 21 9 0 20 \n",
"\n",
" points_per_game points position team \n",
"player \n",
"sergio ag\u00fcero 13.12 209.98 forward manchester city \n",
"alexis s\u00e1nchez 11.19 223.86 forward arsenal \n",
"saido berahino 7.02 147.43 forward west brom "
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Update the `salary` column\n",
"df.update(other=df_2['salary'], overwrite=True)\n",
"df.head(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" player | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" sergio ag\u00fcero | \n",
" 20 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" alexis s\u00e1nchez | \n",
" 15 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"text": [
" salary games goals assists shots_on_target \\\n",
"player \n",
"sergio ag\u00fcero 20 16 14 3 34 \n",
"alexis s\u00e1nchez 15 0 12 7 29 \n",
"saido berahino 13.8 21 9 0 20 \n",
"\n",
" points_per_game points position team \n",
"player \n",
"sergio ag\u00fcero 13.12 209.98 forward manchester city \n",
"alexis s\u00e1nchez 11.19 223.86 forward arsenal \n",
"saido berahino 7.02 147.43 forward west brom "
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Reset the indices\n",
"df.reset_index(inplace=True)\n",
"df.head(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" sergio ag\u00fcero | \n",
" 20 | \n",
" 16 | \n",
" 14 | \n",
" 3 | \n",
" 34 | \n",
" 13.12 | \n",
" 209.98 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 1 | \n",
" alexis s\u00e1nchez | \n",
" 15 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 2 | \n",
" saido berahino | \n",
" 13.8 | \n",
" 21 | \n",
" 9 | \n",
" 0 | \n",
" 20 | \n",
" 7.02 | \n",
" 147.43 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
" player salary games goals assists shots_on_target \\\n",
"0 sergio ag\u00fcero 20 16 14 3 34 \n",
"1 alexis s\u00e1nchez 15 0 12 7 29 \n",
"2 saido berahino 13.8 21 9 0 20 \n",
"\n",
" points_per_game points position team \n",
"0 13.12 209.98 forward manchester city \n",
"1 11.19 223.86 forward arsenal \n",
"2 7.02 147.43 forward west brom "
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chaining Conditions - Using Bitwise Operators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Selecting only those players that either playing for Arsenal or Chelsea\n",
"\n",
"df[ (df['team'] == 'arsenal') | (df['team'] == 'chelsea') ]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" alexis s\u00e1nchez | \n",
" 15 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 3 | \n",
" eden hazard | \n",
" 18.9 | \n",
" 21 | \n",
" 8 | \n",
" 4 | \n",
" 17 | \n",
" 13.05 | \n",
" 274.04 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 7 | \n",
" santiago cazorla | \n",
" 14.8 | \n",
" 20 | \n",
" 4 | \n",
" 0 | \n",
" 20 | \n",
" 9.97 | \n",
" 0.00 | \n",
" midfield | \n",
" arsenal | \n",
"
\n",
" \n",
" 9 | \n",
" cesc f\u00e0bregas | \n",
" 14.0 | \n",
" 20 | \n",
" 2 | \n",
" 14 | \n",
" 10 | \n",
" 10.47 | \n",
" 209.49 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
" player salary games goals assists shots_on_target \\\n",
"1 alexis s\u00e1nchez 15 0 12 7 29 \n",
"3 eden hazard 18.9 21 8 4 17 \n",
"7 santiago cazorla 14.8 20 4 0 20 \n",
"9 cesc f\u00e0bregas 14.0 20 2 14 10 \n",
"\n",
" points_per_game points position team \n",
"1 11.19 223.86 forward arsenal \n",
"3 13.05 274.04 midfield chelsea \n",
"7 9.97 0.00 midfield arsenal \n",
"9 10.47 209.49 midfield chelsea "
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Selecting forwards from Arsenal only\n",
"\n",
"df[ (df['team'] == 'arsenal') & (df['position'] == 'forward') ]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" games | \n",
" goals | \n",
" assists | \n",
" shots_on_target | \n",
" points_per_game | \n",
" points | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" alexis s\u00e1nchez | \n",
" 15 | \n",
" 0 | \n",
" 12 | \n",
" 7 | \n",
" 29 | \n",
" 11.19 | \n",
" 223.86 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
" player salary games goals assists shots_on_target \\\n",
"1 alexis s\u00e1nchez 15 0 12 7 29 \n",
"\n",
" points_per_game points position team \n",
"1 11.19 223.86 forward arsenal "
]
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Column Types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to section overview](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Printing Column Types"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"types = df.columns.to_series().groupby(df.dtypes).groups\n",
"types"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"{dtype('float64'): ['games',\n",
" 'goals',\n",
" 'assists',\n",
" 'shots_on_target',\n",
" 'points_per_game',\n",
" 'points'],\n",
" dtype('O'): ['player', 'salary', 'position', 'team']}"
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Selecting by Column Type"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# select string columns\n",
"df.loc[:, (df.dtypes == np.dtype('O')).values].head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" player | \n",
" salary | \n",
" position | \n",
" team | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" sergio ag\u00fcero | \n",
" 20 | \n",
" forward | \n",
" manchester city | \n",
"
\n",
" \n",
" 1 | \n",
" alexis s\u00e1nchez | \n",
" 15 | \n",
" forward | \n",
" arsenal | \n",
"
\n",
" \n",
" 2 | \n",
" saido berahino | \n",
" 13.8 | \n",
" forward | \n",
" west brom | \n",
"
\n",
" \n",
" 3 | \n",
" eden hazard | \n",
" 18.9 | \n",
" midfield | \n",
" chelsea | \n",
"
\n",
" \n",
" 4 | \n",
" yaya tour\u00e9 | \n",
" 16.6 | \n",
" midfield | \n",
" manchester city | \n",
"
\n",
" \n",
"
\n",
"
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
" player salary position team\n",
"0 sergio ag\u00fcero 20 forward manchester city\n",
"1 alexis s\u00e1nchez 15 forward arsenal\n",
"2 saido berahino 13.8 forward west brom\n",
"3 eden hazard 18.9 midfield chelsea\n",
"4 yaya tour\u00e9 16.6 midfield manchester city"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"
"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Converting Column Types"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df['salary'] = df['salary'].astype(float)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"types = df.columns.to_series().groupby(df.dtypes).groups\n",
"types"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"text": [
"{dtype('float64'): ['salary',\n",
" 'games',\n",
" 'goals',\n",
" 'assists',\n",
" 'shots_on_target',\n",
" 'points_per_game',\n",
" 'points'],\n",
" dtype('O'): ['player', 'position', 'team']}"
]
}
],
"prompt_number": 26
}
],
"metadata": {}
}
]
}