{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove_input"
]
},
"outputs": [],
"source": [
"path_data = '../../data/'\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('fivethirtyeight')\n",
"\n",
"cones = pd.read_csv(path_data + 'cones.csv')\n",
"nba = pd.read_csv(path_data + 'nba_salaries.csv')\n",
"nba.columns=['PLAYER','POSITION','TEAM','SALARY']\n",
"movies = pd.read_csv(path_data + 'movies_by_year.csv')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to DataFrames\n",
"\n",
"We can now apply Python to analyze data. We will work with data stored in DataFrame structures.\n",
"\n",
"A DataFrames (df) is a fundamental way of representing data sets. A df can be viewed in two ways:\n",
"* a sequence of named columns that each describe a single attribute of all entries in a data set, or\n",
"* a sequence of rows that each contain all information about a single individual in a data set.\n",
"\n",
"We will study dfs in great detail in the next several chapters. For now, we will just introduce a few methods without going into technical details. \n",
"\n",
"The df `cones` has been imported for us; later we will see how, but here we will just work with it. First, let's take a look at it."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Color | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" pink | \n",
" 3.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" light brown | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" pink | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Color Price\n",
"0 strawberry pink 3.55\n",
"1 chocolate light brown 4.75\n",
"2 chocolate dark brown 5.25\n",
"3 strawberry pink 5.25\n",
"4 chocolate dark brown 5.25"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The DataFrame has six rows. Each row corresponds to one ice cream cone. The ice cream cones are the *individuals*.\n",
"\n",
"Each cone has three attributes: flavor, color, and price. Each column contains the data on one of these attributes, and so all the entries of any single column are of the same kind. Each column has a label. We will refer to columns by their labels.\n",
"\n",
"A df method is just like a function, but it must operate on a df. So the call looks like\n",
"\n",
"`name_of_DataFrame.method(arguments)`\n",
"\n",
"For example, if you want to see just the first two rows of a df, you can use the df method `head`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Color | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" pink | \n",
" 3.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" light brown | \n",
" 4.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Color Price\n",
"0 strawberry pink 3.55\n",
"1 chocolate light brown 4.75"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.head(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can replace 2 by any number of rows. If you ask for more than six, you will only get six, because `cones` only has six rows."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Choosing Sets of Columns\n",
"The method `select` creates a new table consisting of only the specified columns.\n",
"We can state which columns we want to view by using dot '.' notation (not he same as in maths) or hard brackets with quotes. Note that an index is automatically generated, this is a fundamental aspect of the DataFrame as the index allows us to 'locate' members of the DataFrame."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 strawberry\n",
"1 chocolate\n",
"2 chocolate\n",
"3 strawberry\n",
"4 chocolate\n",
"5 bubblegum\n",
"Name: Flavor, dtype: object"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# single square brackets\n",
"\n",
"cones['Flavor']\n",
"\n",
"# uncomment (remove the hash mark) the line below to view the 'type()' of the output\n",
"\n",
"#type(cones['Flavor'])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
"
\n",
" \n",
" 5 | \n",
" bubblegum | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor\n",
"0 strawberry\n",
"1 chocolate\n",
"2 chocolate\n",
"3 strawberry\n",
"4 chocolate\n",
"5 bubblegum"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# double square brackets\n",
"\n",
"cones[['Flavor']]\n",
"\n",
"# uncomment the line below to view the 'type()' of the output\n",
"\n",
"# type(cones[['Flavor']])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 strawberry\n",
"1 chocolate\n",
"2 chocolate\n",
"3 strawberry\n",
"4 chocolate\n",
"5 bubblegum\n",
"Name: Flavor, dtype: object"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.Flavor"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This leaves the original table unchanged."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Color | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" pink | \n",
" 3.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" light brown | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" pink | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
" 5 | \n",
" bubblegum | \n",
" pink | \n",
" 4.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Color Price\n",
"0 strawberry pink 3.55\n",
"1 chocolate light brown 4.75\n",
"2 chocolate dark brown 5.25\n",
"3 strawberry pink 5.25\n",
"4 chocolate dark brown 5.25\n",
"5 bubblegum pink 4.75"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can select more than one column, by separating the column labels by commas. When you wish to view more than one column the 'hard brackets' must be used twice."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" 3.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" 5.25 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.25 | \n",
"
\n",
" \n",
" 5 | \n",
" bubblegum | \n",
" 4.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price\n",
"0 strawberry 3.55\n",
"1 chocolate 4.75\n",
"2 chocolate 5.25\n",
"3 strawberry 5.25\n",
"4 chocolate 5.25\n",
"5 bubblegum 4.75"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones[['Flavor', 'Price']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also *drop* columns you don't want. The table above can be created by dropping the `Color` column."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" 3.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" 5.25 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.25 | \n",
"
\n",
" \n",
" 5 | \n",
" bubblegum | \n",
" 4.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price\n",
"0 strawberry 3.55\n",
"1 chocolate 4.75\n",
"2 chocolate 5.25\n",
"3 strawberry 5.25\n",
"4 chocolate 5.25\n",
"5 bubblegum 4.75"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.drop(columns=['Color'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can name this new table and look at it again by just typing its name."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" 3.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" 5.25 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.25 | \n",
"
\n",
" \n",
" 5 | \n",
" bubblegum | \n",
" 4.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price\n",
"0 strawberry 3.55\n",
"1 chocolate 4.75\n",
"2 chocolate 5.25\n",
"3 strawberry 5.25\n",
"4 chocolate 5.25\n",
"5 bubblegum 4.75"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"no_colors = cones.drop(columns=['Color'])\n",
"\n",
"no_colors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like selecting columns using hard brackets or dot notation, the `drop` method creates a smaller table and leaves the original table unchanged. In order to explore your data, you can create any number of smaller tables by using choosing or dropping columns. It will do no harm to your original data table."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sorting Rows"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `sort_values` method creates a new table by arranging the rows of the original table in ascending order of the values in the specified column. Here the `cones` table has been sorted in ascending order of the price of the cones.\n",
"\n",
"[pandas sort_values](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html#pandas-dataframe-sort-values)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Color | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" pink | \n",
" 3.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" light brown | \n",
" 4.75 | \n",
"
\n",
" \n",
" 5 | \n",
" bubblegum | \n",
" pink | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" pink | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Color Price\n",
"0 strawberry pink 3.55\n",
"1 chocolate light brown 4.75\n",
"5 bubblegum pink 4.75\n",
"2 chocolate dark brown 5.25\n",
"3 strawberry pink 5.25\n",
"4 chocolate dark brown 5.25"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.sort_values('Price')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To sort in descending order, you can use an *optional* argument to `sort`. As the name implies, optional arguments don't have to be used, but they can be used if you want to change the default behavior of a method. \n",
"\n",
"By default, `sort` sorts in increasing order of the values in the specified column. To sort in decreasing order, use the optional argument `ascending=False`, the default value for `ascending` is `True`."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Color | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 2 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" pink | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" light brown | \n",
" 4.75 | \n",
"
\n",
" \n",
" 5 | \n",
" bubblegum | \n",
" pink | \n",
" 4.75 | \n",
"
\n",
" \n",
" 0 | \n",
" strawberry | \n",
" pink | \n",
" 3.55 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Color Price\n",
"2 chocolate dark brown 5.25\n",
"3 strawberry pink 5.25\n",
"4 chocolate dark brown 5.25\n",
"1 chocolate light brown 4.75\n",
"5 bubblegum pink 4.75\n",
"0 strawberry pink 3.55"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.sort_values('Price', ascending=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As when selecting and `drop`ing the `sort` method leaves the original table unchanged."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Selecting Rows that Satisfy a Condition\n",
"Creating a new DataFrame (in database world this wold be a 'view'), consisting only of the rows that satisfy a given condition we use the 'exactly equal to' `==`. In this section we will work with a very simple condition, which is that the value in a specified column must be exactly equal to a value that we also specify. Thus the `==` method has two arguments.\n",
"\n",
"The code in the cell below creates a df consisting only of the rows corresponding to chocolate cones."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Color | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" chocolate | \n",
" light brown | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" dark brown | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Color Price\n",
"1 chocolate light brown 4.75\n",
"2 chocolate dark brown 5.25\n",
"4 chocolate dark brown 5.25"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones[cones['Flavor']=='chocolate']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The arguments are the label of the column and the value we are looking for in that column. The `==` method can also be used when the condition that the rows must satisfy is more complicated. In those situations the call will be a little more complicated as well.\n",
"\n",
"It is important to provide the value exactly. For example, if we specify `Chocolate` instead of `chocolate`, then `where` correctly finds no rows where the flavor is `Chocolate`."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Color | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: [Flavor, Color, Price]\n",
"Index: []"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones[cones['Flavor'] == 'Chocolate']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like all the other table methods in this section, `==` leaves the original table unchanged."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example: Salaries in the NBA"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\"The NBA is the highest paying professional sports league in the world,\" [reported CNN](http://edition.cnn.com/2015/12/04/sport/gallery/highest-paid-nba-players/) in March 2016. The table `nba` contains the [salaries of all National Basketball Association players](https://www.statcrunch.com/app/index.php?dataid=1843341) in 2015-2016.\n",
"\n",
"Each row represents one player. The columns are:\n",
"\n",
"| **Column Label** | Description |\n",
"|--------------------|-----------------------------------------------------|\n",
"| `PLAYER` | Player's name |\n",
"| `POSITION` | Player's position on team |\n",
"| `TEAM` | Team name |\n",
"|`SALARY` | Player's salary in 2015-2016, in millions of dollars|\n",
" \n",
"The code for the positions is PG (Point Guard), SG (Shooting Guard), PF (Power Forward), SF (Small Forward), and C (Center). But what follows doesn't involve details about how basketball is played.\n",
"\n",
"The first row shows that Paul Millsap, Power Forward for the Atlanta Hawks, had a salary of almost $\\$18.7$ million in 2015-2016."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PLAYER | \n",
" POSITION | \n",
" TEAM | \n",
" SALARY | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Paul Millsap | \n",
" PF | \n",
" Atlanta Hawks | \n",
" 18.671659 | \n",
"
\n",
" \n",
" 1 | \n",
" Al Horford | \n",
" C | \n",
" Atlanta Hawks | \n",
" 12.000000 | \n",
"
\n",
" \n",
" 2 | \n",
" Tiago Splitter | \n",
" C | \n",
" Atlanta Hawks | \n",
" 9.756250 | \n",
"
\n",
" \n",
" 3 | \n",
" Jeff Teague | \n",
" PG | \n",
" Atlanta Hawks | \n",
" 8.000000 | \n",
"
\n",
" \n",
" 4 | \n",
" Kyle Korver | \n",
" SG | \n",
" Atlanta Hawks | \n",
" 5.746479 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 412 | \n",
" Gary Neal | \n",
" PG | \n",
" Washington Wizards | \n",
" 2.139000 | \n",
"
\n",
" \n",
" 413 | \n",
" DeJuan Blair | \n",
" C | \n",
" Washington Wizards | \n",
" 2.000000 | \n",
"
\n",
" \n",
" 414 | \n",
" Kelly Oubre Jr. | \n",
" SF | \n",
" Washington Wizards | \n",
" 1.920240 | \n",
"
\n",
" \n",
" 415 | \n",
" Garrett Temple | \n",
" SG | \n",
" Washington Wizards | \n",
" 1.100602 | \n",
"
\n",
" \n",
" 416 | \n",
" Jarell Eddie | \n",
" SG | \n",
" Washington Wizards | \n",
" 0.561716 | \n",
"
\n",
" \n",
"
\n",
"
417 rows × 4 columns
\n",
"
"
],
"text/plain": [
" PLAYER POSITION TEAM SALARY\n",
"0 Paul Millsap PF Atlanta Hawks 18.671659\n",
"1 Al Horford C Atlanta Hawks 12.000000\n",
"2 Tiago Splitter C Atlanta Hawks 9.756250\n",
"3 Jeff Teague PG Atlanta Hawks 8.000000\n",
"4 Kyle Korver SG Atlanta Hawks 5.746479\n",
".. ... ... ... ...\n",
"412 Gary Neal PG Washington Wizards 2.139000\n",
"413 DeJuan Blair C Washington Wizards 2.000000\n",
"414 Kelly Oubre Jr. SF Washington Wizards 1.920240\n",
"415 Garrett Temple SG Washington Wizards 1.100602\n",
"416 Jarell Eddie SG Washington Wizards 0.561716\n",
"\n",
"[417 rows x 4 columns]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fans of Stephen Curry can find his row by using `where`."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PLAYER | \n",
" POSITION | \n",
" TEAM | \n",
" SALARY | \n",
"
\n",
" \n",
" \n",
" \n",
" 121 | \n",
" Stephen Curry | \n",
" PG | \n",
" Golden State Warriors | \n",
" 11.370786 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" PLAYER POSITION TEAM SALARY\n",
"121 Stephen Curry PG Golden State Warriors 11.370786"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba[nba['PLAYER'] == 'Stephen Curry']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also create a new table called `warriors` consisting of just the data for the Golden State Warriors."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PLAYER | \n",
" POSITION | \n",
" TEAM | \n",
" SALARY | \n",
"
\n",
" \n",
" \n",
" \n",
" 117 | \n",
" Klay Thompson | \n",
" SG | \n",
" Golden State Warriors | \n",
" 15.501000 | \n",
"
\n",
" \n",
" 118 | \n",
" Draymond Green | \n",
" PF | \n",
" Golden State Warriors | \n",
" 14.260870 | \n",
"
\n",
" \n",
" 119 | \n",
" Andrew Bogut | \n",
" C | \n",
" Golden State Warriors | \n",
" 13.800000 | \n",
"
\n",
" \n",
" 120 | \n",
" Andre Iguodala | \n",
" SF | \n",
" Golden State Warriors | \n",
" 11.710456 | \n",
"
\n",
" \n",
" 121 | \n",
" Stephen Curry | \n",
" PG | \n",
" Golden State Warriors | \n",
" 11.370786 | \n",
"
\n",
" \n",
" 122 | \n",
" Jason Thompson | \n",
" PF | \n",
" Golden State Warriors | \n",
" 7.008475 | \n",
"
\n",
" \n",
" 123 | \n",
" Shaun Livingston | \n",
" PG | \n",
" Golden State Warriors | \n",
" 5.543725 | \n",
"
\n",
" \n",
" 124 | \n",
" Harrison Barnes | \n",
" SF | \n",
" Golden State Warriors | \n",
" 3.873398 | \n",
"
\n",
" \n",
" 125 | \n",
" Marreese Speights | \n",
" C | \n",
" Golden State Warriors | \n",
" 3.815000 | \n",
"
\n",
" \n",
" 126 | \n",
" Leandro Barbosa | \n",
" SG | \n",
" Golden State Warriors | \n",
" 2.500000 | \n",
"
\n",
" \n",
" 127 | \n",
" Festus Ezeli | \n",
" C | \n",
" Golden State Warriors | \n",
" 2.008748 | \n",
"
\n",
" \n",
" 128 | \n",
" Brandon Rush | \n",
" SF | \n",
" Golden State Warriors | \n",
" 1.270964 | \n",
"
\n",
" \n",
" 129 | \n",
" Kevon Looney | \n",
" SF | \n",
" Golden State Warriors | \n",
" 1.131960 | \n",
"
\n",
" \n",
" 130 | \n",
" Anderson Varejao | \n",
" PF | \n",
" Golden State Warriors | \n",
" 0.289755 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" PLAYER POSITION TEAM SALARY\n",
"117 Klay Thompson SG Golden State Warriors 15.501000\n",
"118 Draymond Green PF Golden State Warriors 14.260870\n",
"119 Andrew Bogut C Golden State Warriors 13.800000\n",
"120 Andre Iguodala SF Golden State Warriors 11.710456\n",
"121 Stephen Curry PG Golden State Warriors 11.370786\n",
"122 Jason Thompson PF Golden State Warriors 7.008475\n",
"123 Shaun Livingston PG Golden State Warriors 5.543725\n",
"124 Harrison Barnes SF Golden State Warriors 3.873398\n",
"125 Marreese Speights C Golden State Warriors 3.815000\n",
"126 Leandro Barbosa SG Golden State Warriors 2.500000\n",
"127 Festus Ezeli C Golden State Warriors 2.008748\n",
"128 Brandon Rush SF Golden State Warriors 1.270964\n",
"129 Kevon Looney SF Golden State Warriors 1.131960\n",
"130 Anderson Varejao PF Golden State Warriors 0.289755"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"warriors = nba[nba['TEAM'] =='Golden State Warriors']\n",
"warriors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, the first 10 lines of a table are displayed. You can use `head()` to display more or fewer. To display the entire table type the name of the DataFrame."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PLAYER | \n",
" POSITION | \n",
" TEAM | \n",
" SALARY | \n",
"
\n",
" \n",
" \n",
" \n",
" 117 | \n",
" Klay Thompson | \n",
" SG | \n",
" Golden State Warriors | \n",
" 15.501000 | \n",
"
\n",
" \n",
" 118 | \n",
" Draymond Green | \n",
" PF | \n",
" Golden State Warriors | \n",
" 14.260870 | \n",
"
\n",
" \n",
" 119 | \n",
" Andrew Bogut | \n",
" C | \n",
" Golden State Warriors | \n",
" 13.800000 | \n",
"
\n",
" \n",
" 120 | \n",
" Andre Iguodala | \n",
" SF | \n",
" Golden State Warriors | \n",
" 11.710456 | \n",
"
\n",
" \n",
" 121 | \n",
" Stephen Curry | \n",
" PG | \n",
" Golden State Warriors | \n",
" 11.370786 | \n",
"
\n",
" \n",
" 122 | \n",
" Jason Thompson | \n",
" PF | \n",
" Golden State Warriors | \n",
" 7.008475 | \n",
"
\n",
" \n",
" 123 | \n",
" Shaun Livingston | \n",
" PG | \n",
" Golden State Warriors | \n",
" 5.543725 | \n",
"
\n",
" \n",
" 124 | \n",
" Harrison Barnes | \n",
" SF | \n",
" Golden State Warriors | \n",
" 3.873398 | \n",
"
\n",
" \n",
" 125 | \n",
" Marreese Speights | \n",
" C | \n",
" Golden State Warriors | \n",
" 3.815000 | \n",
"
\n",
" \n",
" 126 | \n",
" Leandro Barbosa | \n",
" SG | \n",
" Golden State Warriors | \n",
" 2.500000 | \n",
"
\n",
" \n",
" 127 | \n",
" Festus Ezeli | \n",
" C | \n",
" Golden State Warriors | \n",
" 2.008748 | \n",
"
\n",
" \n",
" 128 | \n",
" Brandon Rush | \n",
" SF | \n",
" Golden State Warriors | \n",
" 1.270964 | \n",
"
\n",
" \n",
" 129 | \n",
" Kevon Looney | \n",
" SF | \n",
" Golden State Warriors | \n",
" 1.131960 | \n",
"
\n",
" \n",
" 130 | \n",
" Anderson Varejao | \n",
" PF | \n",
" Golden State Warriors | \n",
" 0.289755 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" PLAYER POSITION TEAM SALARY\n",
"117 Klay Thompson SG Golden State Warriors 15.501000\n",
"118 Draymond Green PF Golden State Warriors 14.260870\n",
"119 Andrew Bogut C Golden State Warriors 13.800000\n",
"120 Andre Iguodala SF Golden State Warriors 11.710456\n",
"121 Stephen Curry PG Golden State Warriors 11.370786\n",
"122 Jason Thompson PF Golden State Warriors 7.008475\n",
"123 Shaun Livingston PG Golden State Warriors 5.543725\n",
"124 Harrison Barnes SF Golden State Warriors 3.873398\n",
"125 Marreese Speights C Golden State Warriors 3.815000\n",
"126 Leandro Barbosa SG Golden State Warriors 2.500000\n",
"127 Festus Ezeli C Golden State Warriors 2.008748\n",
"128 Brandon Rush SF Golden State Warriors 1.270964\n",
"129 Kevon Looney SF Golden State Warriors 1.131960\n",
"130 Anderson Varejao PF Golden State Warriors 0.289755"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"warriors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `nba` table is sorted in alphabetical order of the team names. To see how the players were paid in 2015-2016, it is useful to sort the data by salary. Remember that by default, the sorting is in increasing order."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PLAYER | \n",
" POSITION | \n",
" TEAM | \n",
" SALARY | \n",
"
\n",
" \n",
" \n",
" \n",
" 267 | \n",
" Thanasis Antetokounmpo | \n",
" SF | \n",
" New York Knicks | \n",
" 0.030888 | \n",
"
\n",
" \n",
" 327 | \n",
" Cory Jefferson | \n",
" PF | \n",
" Phoenix Suns | \n",
" 0.049709 | \n",
"
\n",
" \n",
" 326 | \n",
" Jordan McRae | \n",
" SG | \n",
" Phoenix Suns | \n",
" 0.049709 | \n",
"
\n",
" \n",
" 324 | \n",
" Orlando Johnson | \n",
" SG | \n",
" Phoenix Suns | \n",
" 0.055722 | \n",
"
\n",
" \n",
" 325 | \n",
" Phil Pressey | \n",
" PG | \n",
" Phoenix Suns | \n",
" 0.055722 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 131 | \n",
" Dwight Howard | \n",
" C | \n",
" Houston Rockets | \n",
" 22.359364 | \n",
"
\n",
" \n",
" 255 | \n",
" Carmelo Anthony | \n",
" SF | \n",
" New York Knicks | \n",
" 22.875000 | \n",
"
\n",
" \n",
" 72 | \n",
" LeBron James | \n",
" SF | \n",
" Cleveland Cavaliers | \n",
" 22.970500 | \n",
"
\n",
" \n",
" 29 | \n",
" Joe Johnson | \n",
" SF | \n",
" Brooklyn Nets | \n",
" 24.894863 | \n",
"
\n",
" \n",
" 169 | \n",
" Kobe Bryant | \n",
" SF | \n",
" Los Angeles Lakers | \n",
" 25.000000 | \n",
"
\n",
" \n",
"
\n",
"
417 rows × 4 columns
\n",
"
"
],
"text/plain": [
" PLAYER POSITION TEAM SALARY\n",
"267 Thanasis Antetokounmpo SF New York Knicks 0.030888\n",
"327 Cory Jefferson PF Phoenix Suns 0.049709\n",
"326 Jordan McRae SG Phoenix Suns 0.049709\n",
"324 Orlando Johnson SG Phoenix Suns 0.055722\n",
"325 Phil Pressey PG Phoenix Suns 0.055722\n",
".. ... ... ... ...\n",
"131 Dwight Howard C Houston Rockets 22.359364\n",
"255 Carmelo Anthony SF New York Knicks 22.875000\n",
"72 LeBron James SF Cleveland Cavaliers 22.970500\n",
"29 Joe Johnson SF Brooklyn Nets 24.894863\n",
"169 Kobe Bryant SF Los Angeles Lakers 25.000000\n",
"\n",
"[417 rows x 4 columns]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba.sort_values('SALARY')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These figures are somewhat difficult to compare as some of these players changed teams during the season and received salaries from more than one team; only the salary from the last team appears in the table. \n",
"\n",
"The CNN report is about the other end of the salary scale – the players who are among the highest paid in the world. To identify these players we can sort in descending order of salary and look at the top few rows."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PLAYER | \n",
" POSITION | \n",
" TEAM | \n",
" SALARY | \n",
"
\n",
" \n",
" \n",
" \n",
" 169 | \n",
" Kobe Bryant | \n",
" SF | \n",
" Los Angeles Lakers | \n",
" 25.000000 | \n",
"
\n",
" \n",
" 29 | \n",
" Joe Johnson | \n",
" SF | \n",
" Brooklyn Nets | \n",
" 24.894863 | \n",
"
\n",
" \n",
" 72 | \n",
" LeBron James | \n",
" SF | \n",
" Cleveland Cavaliers | \n",
" 22.970500 | \n",
"
\n",
" \n",
" 255 | \n",
" Carmelo Anthony | \n",
" SF | \n",
" New York Knicks | \n",
" 22.875000 | \n",
"
\n",
" \n",
" 131 | \n",
" Dwight Howard | \n",
" C | \n",
" Houston Rockets | \n",
" 22.359364 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 200 | \n",
" Elliot Williams | \n",
" SG | \n",
" Memphis Grizzlies | \n",
" 0.055722 | \n",
"
\n",
" \n",
" 324 | \n",
" Orlando Johnson | \n",
" SG | \n",
" Phoenix Suns | \n",
" 0.055722 | \n",
"
\n",
" \n",
" 327 | \n",
" Cory Jefferson | \n",
" PF | \n",
" Phoenix Suns | \n",
" 0.049709 | \n",
"
\n",
" \n",
" 326 | \n",
" Jordan McRae | \n",
" SG | \n",
" Phoenix Suns | \n",
" 0.049709 | \n",
"
\n",
" \n",
" 267 | \n",
" Thanasis Antetokounmpo | \n",
" SF | \n",
" New York Knicks | \n",
" 0.030888 | \n",
"
\n",
" \n",
"
\n",
"
417 rows × 4 columns
\n",
"
"
],
"text/plain": [
" PLAYER POSITION TEAM SALARY\n",
"169 Kobe Bryant SF Los Angeles Lakers 25.000000\n",
"29 Joe Johnson SF Brooklyn Nets 24.894863\n",
"72 LeBron James SF Cleveland Cavaliers 22.970500\n",
"255 Carmelo Anthony SF New York Knicks 22.875000\n",
"131 Dwight Howard C Houston Rockets 22.359364\n",
".. ... ... ... ...\n",
"200 Elliot Williams SG Memphis Grizzlies 0.055722\n",
"324 Orlando Johnson SG Phoenix Suns 0.055722\n",
"327 Cory Jefferson PF Phoenix Suns 0.049709\n",
"326 Jordan McRae SG Phoenix Suns 0.049709\n",
"267 Thanasis Antetokounmpo SF New York Knicks 0.030888\n",
"\n",
"[417 rows x 4 columns]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba.sort_values('SALARY', ascending=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kobe Bryant, since retired, was the highest earning NBA player in 2015-2016."
]
}
],
"metadata": {
"anaconda-cloud": {},
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}