{ "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" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "remove_input" ] }, "outputs": [], "source": [ "nba_salaries = pd.read_csv(path_data + 'nba_salaries.csv')\n", "nba = nba_salaries.rename(columns={\"'15-'16 SALARY\": 'SALARY'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Selecting Rows\n", "\n", "Often, we would like to extract just those rows that correspond to entries with a particular feature. For example, we might want only the rows corresponding to the Warriors, or to players who earned more than $\\$10$ million. Or we might just want the top five earners." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Specified Rows\n", "The fact that a DataFrame creates an index by default startts to become very useful here as we can specify which rows (by default) we wish to inspect by stating an index or an index range. The argument used a row index or array of indices, and it creates a new DataFrame consisting of only those rows.\n", "\n", "For example, if we wanted just the first row of `nba`, we could use `df.iloc[]` as follows." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
0Paul MillsapPFAtlanta Hawks18.671659
1Al HorfordCAtlanta Hawks12.000000
2Tiago SplitterCAtlanta Hawks9.756250
3Jeff TeaguePGAtlanta Hawks8.000000
4Kyle KorverSGAtlanta Hawks5.746479
...............
412Gary NealPGWashington Wizards2.139000
413DeJuan BlairCWashington Wizards2.000000
414Kelly Oubre Jr.SFWashington Wizards1.920240
415Garrett TempleSGWashington Wizards1.100602
416Jarell EddieSGWashington Wizards0.561716
\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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PLAYER Paul Millsap\n", "POSITION PF\n", "TEAM Atlanta Hawks\n", "SALARY 18.6717\n", "Name: 0, dtype: object" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba.iloc[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PLAYERPOSITIONTEAMSALARY
0Paul MillsapPFAtlanta Hawks18.671659
\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "0 Paul Millsap PF Atlanta Hawks 18.671659" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba.iloc[[0]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a new table with just the single row that we specified.\n", "\n", "We could also get the fourth, fifth, and sixth rows by specifying a range of indices as the argument." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
3Jeff TeaguePGAtlanta Hawks8.000000
4Kyle KorverSGAtlanta Hawks5.746479
5Thabo SefoloshaSFAtlanta Hawks4.000000
\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "3 Jeff Teague PG Atlanta Hawks 8.000000\n", "4 Kyle Korver SG Atlanta Hawks 5.746479\n", "5 Thabo Sefolosha SF Atlanta Hawks 4.000000" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba.iloc[np.arange(3, 6)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want a table of the top 5 highest paid players, we can first sort the list by salary and then `df.iloc[]` the first five rows:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
169Kobe BryantSFLos Angeles Lakers25.000000
29Joe JohnsonSFBrooklyn Nets24.894863
72LeBron JamesSFCleveland Cavaliers22.970500
255Carmelo AnthonySFNew York Knicks22.875000
131Dwight HowardCHouston Rockets22.359364
\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" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba.sort_values('SALARY', ascending=False).iloc[(np.arange(5))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rows Corresponding to a Specified Feature\n", "More often, we will want to access data in a set of rows that have a certain feature, but whose indices we don't know ahead of time. For example, we might want data on all the players who made more than $\\$10$ million, but we don't want to spend time counting rows in the sorted table.\n", "\n", "Array version - if we wish to work with an array we can use `np.where(df['column'] criteria)`. \n", "\n", "[np.where()](https://numpy.org/doc/stable/reference/generated/numpy.where.html)\n", "\n", "DataFrame version - to implement a selection criteria the df is called with selection criteria being applied to the df.col i.e. `df[df['column_name']criteria]`.\n", "\n", "In the first example, we extract the data for all those who earned more than $\\$10$ million." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([ 0, 1, 29, 30, 42, 43, 44, 60, 61, 62, 72, 73, 74,\n", " 75, 76, 82, 83, 93, 94, 95, 107, 117, 118, 119, 120, 121,\n", " 131, 132, 133, 143, 144, 156, 157, 169, 170, 180, 201, 202, 203,\n", " 204, 213, 226, 227, 239, 240, 241, 255, 256, 268, 269, 270, 271,\n", " 284, 285, 298, 311, 312, 342, 343, 353, 354, 355, 366, 367, 368,\n", " 383, 400, 401, 402]),)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(nba['SALARY'] > 10)\n", "\n", "# or - this is an example of alternatives being available to select,\n", "# this may depend upon preference, the task at hand, the impact of processing time or export requirements\n", "\n", "#nba[nba['SALARY'] >10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The use of the argument `df[df[col] > 10]` ensured that each selected row had a value of `SALARY` that was greater than 10.\n", "\n", "There are 69 rows in the new table, corresponding to the 69 players who made more than $10$ million dollars. Arranging these rows in order makes the data easier to analyze. DeMar DeRozan of the Toronto Raptors was the \"poorest\" of this group, at a salary of just over $10$ million dollars." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
368DeMar DeRozanSGToronto Raptors10.050000
298Gerald WallaceSFPhiladelphia 76ers10.105855
204Luol DengSFMiami Heat10.151612
144Monta EllisSGIndiana Pacers10.300000
95Wilson ChandlerSFDenver Nuggets10.449438
...............
131Dwight HowardCHouston Rockets22.359364
255Carmelo AnthonySFNew York Knicks22.875000
72LeBron JamesSFCleveland Cavaliers22.970500
29Joe JohnsonSFBrooklyn Nets24.894863
169Kobe BryantSFLos Angeles Lakers25.000000
\n", "

69 rows × 4 columns

\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "368 DeMar DeRozan SG Toronto Raptors 10.050000\n", "298 Gerald Wallace SF Philadelphia 76ers 10.105855\n", "204 Luol Deng SF Miami Heat 10.151612\n", "144 Monta Ellis SG Indiana Pacers 10.300000\n", "95 Wilson Chandler SF Denver Nuggets 10.449438\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", "[69 rows x 4 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[nba['SALARY'] >10].sort_values('SALARY')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How much did Stephen Curry make? For the answer, we have to access the row where the value of `PLAYER` is equal to `Stephen Curry`. That is placed a table consisting of just one line:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PLAYERPOSITIONTEAMSALARY
121Stephen CurryPGGolden State Warriors11.370786
\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "121 Stephen Curry PG Golden State Warriors 11.370786" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[nba['PLAYER'] == 'Stephen Curry']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Curry made just under $\\$11.4$ million dollars. That's a lot of money, but it's less than half the salary of LeBron James. You'll find that salary in the \"Top 5\" table earlier in this section, or you could find it replacing `'Stephen Curry'` by `'LeBron James'` in the line of code above.\n", "\n", "Thus for example you can get a DataFrame where the 'TEAM' is exactly equal to 'Golden State Warriors':" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
117Klay ThompsonSGGolden State Warriors15.501000
118Draymond GreenPFGolden State Warriors14.260870
119Andrew BogutCGolden State Warriors13.800000
120Andre IguodalaSFGolden State Warriors11.710456
121Stephen CurryPGGolden State Warriors11.370786
122Jason ThompsonPFGolden State Warriors7.008475
123Shaun LivingstonPGGolden State Warriors5.543725
124Harrison BarnesSFGolden State Warriors3.873398
125Marreese SpeightsCGolden State Warriors3.815000
126Leandro BarbosaSGGolden State Warriors2.500000
127Festus EzeliCGolden State Warriors2.008748
128Brandon RushSFGolden State Warriors1.270964
129Kevon LooneySFGolden State Warriors1.131960
130Anderson VarejaoPFGolden State Warriors0.289755
\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": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[nba['TEAM'] == 'Golden State Warriors']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This portion of the table is already sorted by salary, because the original table listed players sorted by salary within the same team. By not using `.head()` at the end of the line all rows are shown, not just the first 10." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiple Features\n", "You can access rows that have multiple specified features, by using the boolean `&` operator. For example, here is a way to extract all the Point Guards whose salaries were over $\\$15$ million." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
60Derrick RosePGChicago Bulls20.093064
74Kyrie IrvingPGCleveland Cavaliers16.407501
156Chris PaulPGLos Angeles Clippers21.468695
269Russell WestbrookPGOklahoma City Thunder16.744218
400John WallPGWashington Wizards15.851950
\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "60 Derrick Rose PG Chicago Bulls 20.093064\n", "74 Kyrie Irving PG Cleveland Cavaliers 16.407501\n", "156 Chris Paul PG Los Angeles Clippers 21.468695\n", "269 Russell Westbrook PG Oklahoma City Thunder 16.744218\n", "400 John Wall PG Washington Wizards 15.851950" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[(nba['POSITION'] == 'PG') & (nba['SALARY'] > 15)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### General Form\n", "By now you will have realized that the general way to create a new df by selecting rows with a given feature is to use `&` or `OR` with the appropriate condition:\n", "\n", "`df[df['column_label_string'] condition(<, >, ==, =>, etc) criteria]`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
144Monta EllisSGIndiana Pacers10.300000
204Luol DengSFMiami Heat10.151612
298Gerald WallaceSFPhiladelphia 76ers10.105855
356Danny GreenSGSan Antonio Spurs10.000000
368DeMar DeRozanSGToronto Raptors10.050000
\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "144 Monta Ellis SG Indiana Pacers 10.300000\n", "204 Luol Deng SF Miami Heat 10.151612\n", "298 Gerald Wallace SF Philadelphia 76ers 10.105855\n", "356 Danny Green SG San Antonio Spurs 10.000000\n", "368 DeMar DeRozan SG Toronto Raptors 10.050000" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[(nba['SALARY'] >= 10) & (nba['SALARY'] <=10.3)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we specify a condition that isn't satisfied by any row, we get a table with column labels but no rows." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PLAYERPOSITIONTEAMSALARY
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [PLAYER, POSITION, TEAM, SALARY]\n", "Index: []" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[nba['PLAYER'] == 'Barack Obama']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We end the section with a series of examples. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The use of `are.containing` can help save some typing. For example, you can just specify `Warriors` instead of `Golden State Warriors`:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
117Klay ThompsonSGGolden State Warriors15.501000
118Draymond GreenPFGolden State Warriors14.260870
119Andrew BogutCGolden State Warriors13.800000
120Andre IguodalaSFGolden State Warriors11.710456
121Stephen CurryPGGolden State Warriors11.370786
122Jason ThompsonPFGolden State Warriors7.008475
123Shaun LivingstonPGGolden State Warriors5.543725
124Harrison BarnesSFGolden State Warriors3.873398
125Marreese SpeightsCGolden State Warriors3.815000
126Leandro BarbosaSGGolden State Warriors2.500000
127Festus EzeliCGolden State Warriors2.008748
128Brandon RushSFGolden State Warriors1.270964
129Kevon LooneySFGolden State Warriors1.131960
130Anderson VarejaoPFGolden State Warriors0.289755
\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": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[nba['TEAM'].str.contains('Warriors')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can extract data for all the guards, both Point Guards and Shooting Guards:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
3Jeff TeaguePGAtlanta Hawks8.000000
4Kyle KorverSGAtlanta Hawks5.746479
8Dennis SchroderPGAtlanta Hawks1.763400
9Tim Hardaway Jr.SGAtlanta Hawks1.304520
11Jason RichardsonSGAtlanta Hawks0.947276
...............
409Alan AndersonSGWashington Wizards4.000000
411Ramon SessionsPGWashington Wizards2.170465
412Gary NealPGWashington Wizards2.139000
415Garrett TempleSGWashington Wizards1.100602
416Jarell EddieSGWashington Wizards0.561716
\n", "

181 rows × 4 columns

\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "3 Jeff Teague PG Atlanta Hawks 8.000000\n", "4 Kyle Korver SG Atlanta Hawks 5.746479\n", "8 Dennis Schroder PG Atlanta Hawks 1.763400\n", "9 Tim Hardaway Jr. SG Atlanta Hawks 1.304520\n", "11 Jason Richardson SG Atlanta Hawks 0.947276\n", ".. ... ... ... ...\n", "409 Alan Anderson SG Washington Wizards 4.000000\n", "411 Ramon Sessions PG Washington Wizards 2.170465\n", "412 Gary Neal PG Washington Wizards 2.139000\n", "415 Garrett Temple SG Washington Wizards 1.100602\n", "416 Jarell Eddie SG Washington Wizards 0.561716\n", "\n", "[181 rows x 4 columns]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nba[nba['POSITION'].str.contains('G')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get all the players who were not Cleveland Cavaliers and had a salary of no less than $\\$20$ million:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/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", "
PLAYERPOSITIONTEAMSALARY
29Joe JohnsonSFBrooklyn Nets24.894863
60Derrick RosePGChicago Bulls20.093064
131Dwight HowardCHouston Rockets22.359364
156Chris PaulPGLos Angeles Clippers21.468695
169Kobe BryantSFLos Angeles Lakers25.000000
201Chris BoshPFMiami Heat22.192730
255Carmelo AnthonySFNew York Knicks22.875000
268Kevin DurantSFOklahoma City Thunder20.158622
\n", "
" ], "text/plain": [ " PLAYER POSITION TEAM SALARY\n", "29 Joe Johnson SF Brooklyn Nets 24.894863\n", "60 Derrick Rose PG Chicago Bulls 20.093064\n", "131 Dwight Howard C Houston Rockets 22.359364\n", "156 Chris Paul PG Los Angeles Clippers 21.468695\n", "169 Kobe Bryant SF Los Angeles Lakers 25.000000\n", "201 Chris Bosh PF Miami Heat 22.192730\n", "255 Carmelo Anthony SF New York Knicks 22.875000\n", "268 Kevin Durant SF Oklahoma City Thunder 20.158622" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "other_than_Cavs = nba[nba['TEAM'] != 'Cleveland Cavaliers']\n", "other_than_Cavs[other_than_Cavs['SALARY'] > 20]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same table can be created in many ways. Here is another, and no doubt you can think of more." ] } ], "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 }