{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove_input"
]
},
"outputs": [],
"source": [
"path_data = '../../data/'\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('fivethirtyeight')\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Classifying by One Variable\n",
"\n",
"Data scientists often need to classify individuals into groups according to shared features, and then identify some characteristics of the groups. For example, in the example using Galton's data on heights, we saw that it was useful to classify families according to the parents' midparent heights, and then find the average height of the children in each group.\n",
"\n",
"This section is about classifying individuals into categories that are not numerical. We begin by recalling the basic use of `group`. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Counting the Number in Each Category\n",
"The `group` method with a single argument counts the number of rows for each category in a column. The result contains one row per unique value in the grouped column.\n",
"\n",
"Here is a small table of data on ice cream cones. The `group` method can be used to list the distinct flavors and provide the counts of each flavor."
]
},
{
"cell_type": "code",
"execution_count": 19,
"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",
" 6.55 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" 5.25 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price\n",
"0 strawberry 3.55\n",
"1 chocolate 4.75\n",
"2 chocolate 6.55\n",
"3 strawberry 5.25\n",
"4 chocolate 5.25"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones = pd.DataFrame({\n",
" 'Flavor':np.array(['strawberry', 'chocolate', 'chocolate', 'strawberry', 'chocolate']),\n",
" 'Price':np.array([3.55, 4.75, 6.55, 5.25, 5.25])}\n",
")\n",
"cones"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" count | \n",
"
\n",
" \n",
" Flavor | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | \n",
" 3 | \n",
"
\n",
" \n",
" strawberry | \n",
" 2 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" count\n",
"Flavor \n",
"chocolate 3\n",
"strawberry 2"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_grouped = cones.groupby([\"Flavor\"]).agg(\n",
" count=pd.NamedAgg(column=\"Flavor\", aggfunc=\"count\")\n",
")\n",
"\n",
"df_grouped"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are two distinct categories, chocolate and strawberry. When we call `groupby` we must state what we want to do with the group data e.g. `count()`. Applying the `count()` method will create a column of counts whcih takes the names of the first column in the df by default, and contains the number of rows in each category. To make this easier to read we could change the count column to 'count'.\n",
"\n",
"Notice that this can all be worked out from just the `Flavor` column. Only the `Price` column name has been used, the data has not been used.\n",
"\n",
"But what if we wanted the total price of the cones of each different flavor? In this case we can apply a different method e.g. `sum()`, to `groupby`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Finding a Characteristic of Each Category\n",
"The optional second argument of `group` names the function that will be used to aggregate values in other columns for all of those rows. For instance, `sum` will sum up the prices in all rows that match each category. This result also contains one row per unique value in the grouped column, but it has the same number of columns as the original table.\n",
"\n",
"To find the total price of each flavor, we call `group` again, with `Flavor` as its first argument as before. But this time there is a second argument: the function name `sum`."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Price_sum | \n",
"
\n",
" \n",
" Flavor | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | \n",
" 16.55 | \n",
"
\n",
" \n",
" strawberry | \n",
" 8.80 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Price_sum\n",
"Flavor \n",
"chocolate 16.55\n",
"strawberry 8.80"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_grouped_by_price = cones.groupby([\"Flavor\"]).agg(\n",
" Price_sum=pd.NamedAgg(column=\"Price\", aggfunc=\"sum\")\n",
")\n",
"\n",
"df_grouped_by_price"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create this new table, `groupby` has calculated the **sum** of the `Price` entries in all the rows corresponding to each distinct flavor. The prices in the three `chocolate` rows add up to 16.55 (in whatever currency). The prices in the two `strawberry` rows have a total of 8.80.\n",
"\n",
"Using pandas `groupby aggregation` we can compute a summary statistic (or statistics). The label of the newly created column is `Price sum`, which is created by taking the label of the column being summed, and appending the word `sum` in the *aggregation pipeline*. \n",
"\n",
"In this insatnce there are only two columns so when `group` finds the `sum` of all columns other than the one with the categories, there is no need to specify that it has to `sum` the prices. Using the [`Pandas NamedAgg`](https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#aggregation) function we can name the column contining the results of the aggregation.\n",
"\n",
"To see in more detail what `group` is doing, notice that you could have figured out the total prices yourself, not only by mental arithmetic but also using code. For example, to find the total price of all the chocolate cones, you could start by creating a new table consisting of only the chocolate cones, and then accessing the column of prices:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 4.75\n",
"2 6.55\n",
"4 5.25\n",
"Name: Price, dtype: float64"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones[cones['Flavor'] == 'chocolate']['Price']"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 4.75 | \n",
"
\n",
" \n",
" 2 | \n",
" 6.55 | \n",
"
\n",
" \n",
" 4 | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Price\n",
"1 4.75\n",
"2 6.55\n",
"4 5.25"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones[cones['Flavor'] == 'chocolate'][['Price']]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Price 16.55\n",
"dtype: float64"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sum(cones[cones['Flavor'] == 'chocolate'][['Price']])"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16.55"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sum([cones[cones['Flavor'] == 'chocolate'][['Price']]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is what `groupby` is doing for each distinct value in `Flavor`."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Array of All the Prices | \n",
" Sum of the Array | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" chocolate | \n",
" [4.75, 6.55, 5.25] | \n",
" 16.55 | \n",
"
\n",
" \n",
" 1 | \n",
" strawberry | \n",
" [3.55, 5.25] | \n",
" 8.80 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Array of All the Prices Sum of the Array\n",
"0 chocolate [4.75, 6.55, 5.25] 16.55\n",
"1 strawberry [3.55, 5.25] 8.80"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# For each distinct value in `Flavor, access all the rows\n",
"# and create an array of `Price`\n",
"\n",
"cones_choc = cones[cones['Flavor'] == 'chocolate']['Price']\n",
"\n",
"cones_strawb = cones[cones['Flavor'] =='strawberry']['Price']\n",
"\n",
"# Display the arrays in a table\n",
"\n",
"cones_choc = np.array(cones_choc)\n",
"cones_strawb = np.array(cones_strawb)\n",
"\n",
"grouped_cones = pd.DataFrame({\n",
" 'Flavor':np.array(['chocolate', 'strawberry']),\n",
" 'Array of All the Prices':[cones_choc, cones_strawb]}\n",
")\n",
"\n",
"#priceTotals\n",
"\n",
"# Append a column with the sum of the `Price` values in each array\n",
"\n",
"price_totals = grouped_cones\n",
"\n",
"price_totals['Sum of the Array'] = np.array([sum(cones_choc), sum(cones_strawb)])\n",
"\n",
"price_totals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can replace `sum` by any other functions that work on arrays. For example, you could use `max` to find the largest price in each category:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Price | \n",
"
\n",
" \n",
" Flavor | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | \n",
" 6.55 | \n",
"
\n",
" \n",
" strawberry | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Price\n",
"Flavor \n",
"chocolate 6.55\n",
"strawberry 5.25"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.groupby('Flavor').max()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### *Or*"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Price_Max | \n",
"
\n",
" \n",
" Flavor | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | \n",
" 6.55 | \n",
"
\n",
" \n",
" strawberry | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Price_Max\n",
"Flavor \n",
"chocolate 6.55\n",
"strawberry 5.25"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"price_max = cones.groupby([\"Flavor\"]).agg(\n",
" Price_Max=pd.NamedAgg(column=\"Price\", aggfunc=\"max\")\n",
")\n",
"\n",
"price_max"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once again, `groupby` creates arrays of the prices in each `Flavor` category. But now it finds the `max` of each array:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Array of All the Prices | \n",
" Sum of the Array | \n",
" Max of the Array | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" chocolate | \n",
" [4.75, 6.55, 5.25] | \n",
" 16.55 | \n",
" 6.55 | \n",
"
\n",
" \n",
" 1 | \n",
" strawberry | \n",
" [3.55, 5.25] | \n",
" 8.80 | \n",
" 5.25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Array of All the Prices Sum of the Array Max of the Array\n",
"0 chocolate [4.75, 6.55, 5.25] 16.55 6.55\n",
"1 strawberry [3.55, 5.25] 8.80 5.25"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"price_max = grouped_cones.copy()\n",
"\n",
"price_max['Max of the Array'] = np.array([max(cones_choc), max(cones_strawb)])\n",
"\n",
"price_max"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indeed, the original call to `group` with just one argument has the same effect as using `len` as the function and then cleaning up the table."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Array of All the Prices | \n",
" Sum of the Array | \n",
" Length of the Array | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" chocolate | \n",
" [4.75, 6.55, 5.25] | \n",
" 16.55 | \n",
" 3 | \n",
"
\n",
" \n",
" 1 | \n",
" strawberry | \n",
" [3.55, 5.25] | \n",
" 8.80 | \n",
" 2 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Array of All the Prices Sum of the Array Length of the Array\n",
"0 chocolate [4.75, 6.55, 5.25] 16.55 3\n",
"1 strawberry [3.55, 5.25] 8.80 2"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array_length = grouped_cones.copy()\n",
"\n",
"array_length['Length of the Array'] = np.array([len(cones_choc), len(cones_strawb)])\n",
"\n",
"array_length"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example: NBA Salaries\n",
"The table `nba` contains data on the 2015-2016 players in the National Basketball Association. We have examined these data earlier. Recall that salaries are measured in millions of dollars."
]
},
{
"cell_type": "code",
"execution_count": 31,
"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": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba1 = pd.read_csv(path_data + 'nba_salaries.csv')\n",
"\n",
"nba = nba1.rename(columns={\"'15-'16 SALARY\": 'SALARY'})\n",
"\n",
"nba"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1.** How much money did each team pay for its players' salaries?\n",
"\n",
"The only columns involved are `TEAM` and `SALARY`. We have to `group` the rows by `TEAM` and then `sum` the salaries of the groups. "
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" SALARY | \n",
"
\n",
" \n",
" TEAM | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" Atlanta Hawks | \n",
" 69.573103 | \n",
"
\n",
" \n",
" Boston Celtics | \n",
" 50.285499 | \n",
"
\n",
" \n",
" Brooklyn Nets | \n",
" 57.306976 | \n",
"
\n",
" \n",
" Charlotte Hornets | \n",
" 84.102397 | \n",
"
\n",
" \n",
" Chicago Bulls | \n",
" 78.820890 | \n",
"
\n",
" \n",
" Cleveland Cavaliers | \n",
" 102.312412 | \n",
"
\n",
" \n",
" Dallas Mavericks | \n",
" 65.762559 | \n",
"
\n",
" \n",
" Denver Nuggets | \n",
" 62.429404 | \n",
"
\n",
" \n",
" Detroit Pistons | \n",
" 42.211760 | \n",
"
\n",
" \n",
" Golden State Warriors | \n",
" 94.085137 | \n",
"
\n",
" \n",
" Houston Rockets | \n",
" 85.285837 | \n",
"
\n",
" \n",
" Indiana Pacers | \n",
" 62.695023 | \n",
"
\n",
" \n",
" Los Angeles Clippers | \n",
" 66.074113 | \n",
"
\n",
" \n",
" Los Angeles Lakers | \n",
" 68.607944 | \n",
"
\n",
" \n",
" Memphis Grizzlies | \n",
" 93.796439 | \n",
"
\n",
" \n",
" Miami Heat | \n",
" 81.528667 | \n",
"
\n",
" \n",
" Milwaukee Bucks | \n",
" 52.258355 | \n",
"
\n",
" \n",
" Minnesota Timberwolves | \n",
" 65.847421 | \n",
"
\n",
" \n",
" New Orleans Pelicans | \n",
" 80.514606 | \n",
"
\n",
" \n",
" New York Knicks | \n",
" 69.404994 | \n",
"
\n",
" \n",
" Oklahoma City Thunder | \n",
" 96.832165 | \n",
"
\n",
" \n",
" Orlando Magic | \n",
" 77.623940 | \n",
"
\n",
" \n",
" Philadelphia 76ers | \n",
" 42.481345 | \n",
"
\n",
" \n",
" Phoenix Suns | \n",
" 50.520815 | \n",
"
\n",
" \n",
" Portland Trail Blazers | \n",
" 45.446878 | \n",
"
\n",
" \n",
" Sacramento Kings | \n",
" 68.384890 | \n",
"
\n",
" \n",
" San Antonio Spurs | \n",
" 84.652074 | \n",
"
\n",
" \n",
" Toronto Raptors | \n",
" 74.672620 | \n",
"
\n",
" \n",
" Utah Jazz | \n",
" 52.631878 | \n",
"
\n",
" \n",
" Washington Wizards | \n",
" 90.047498 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" SALARY\n",
"TEAM \n",
"Atlanta Hawks 69.573103\n",
"Boston Celtics 50.285499\n",
"Brooklyn Nets 57.306976\n",
"Charlotte Hornets 84.102397\n",
"Chicago Bulls 78.820890\n",
"Cleveland Cavaliers 102.312412\n",
"Dallas Mavericks 65.762559\n",
"Denver Nuggets 62.429404\n",
"Detroit Pistons 42.211760\n",
"Golden State Warriors 94.085137\n",
"Houston Rockets 85.285837\n",
"Indiana Pacers 62.695023\n",
"Los Angeles Clippers 66.074113\n",
"Los Angeles Lakers 68.607944\n",
"Memphis Grizzlies 93.796439\n",
"Miami Heat 81.528667\n",
"Milwaukee Bucks 52.258355\n",
"Minnesota Timberwolves 65.847421\n",
"New Orleans Pelicans 80.514606\n",
"New York Knicks 69.404994\n",
"Oklahoma City Thunder 96.832165\n",
"Orlando Magic 77.623940\n",
"Philadelphia 76ers 42.481345\n",
"Phoenix Suns 50.520815\n",
"Portland Trail Blazers 45.446878\n",
"Sacramento Kings 68.384890\n",
"San Antonio Spurs 84.652074\n",
"Toronto Raptors 74.672620\n",
"Utah Jazz 52.631878\n",
"Washington Wizards 90.047498"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"teams_and_money = nba[['TEAM', 'SALARY']]\n",
"\n",
"teams_and_money.groupby('TEAM').sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2.** How many NBA players were there in each of the five positions?\n",
"\n",
"We have to classify by `POSITION`, and count. This can be achieved by applying the `count()` method to a `groupby` or and the aggregation method with `aggfunc=\"count\"`."
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" count | \n",
"
\n",
" \n",
" POSITION | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" C | \n",
" 69 | \n",
"
\n",
" \n",
" PF | \n",
" 85 | \n",
"
\n",
" \n",
" PG | \n",
" 85 | \n",
"
\n",
" \n",
" SF | \n",
" 82 | \n",
"
\n",
" \n",
" SG | \n",
" 96 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" count\n",
"POSITION \n",
"C 69\n",
"PF 85\n",
"PG 85\n",
"SF 82\n",
"SG 96"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#nba.groupby('POSITION').count()\n",
"\n",
"# -- or\n",
"\n",
"position_count = nba.groupby([\"POSITION\"]).agg(\n",
" count=pd.NamedAgg(column=\"PLAYER\", aggfunc=\"count\")\n",
")\n",
"\n",
"position_count"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3.** What was the average salary of the players at each of the five positions?\n",
"\n",
"This time, we have to group by `POSITION` and take the mean of the salaries. For clarity, we will work with a table of just the positions and the salaries."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" SALARY | \n",
"
\n",
" \n",
" POSITION | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" C | \n",
" 6.082913 | \n",
"
\n",
" \n",
" PF | \n",
" 4.951344 | \n",
"
\n",
" \n",
" PG | \n",
" 5.165487 | \n",
"
\n",
" \n",
" SF | \n",
" 5.532675 | \n",
"
\n",
" \n",
" SG | \n",
" 3.988195 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" SALARY\n",
"POSITION \n",
"C 6.082913\n",
"PF 4.951344\n",
"PG 5.165487\n",
"SF 5.532675\n",
"SG 3.988195"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"positions_and_money = nba[['POSITION', 'SALARY']]\n",
"\n",
"positions_and_money.groupby('POSITION').mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Center was the most highly paid position, at an average of over 6 million dollars.\n",
"\n",
"If we had not selected the two columns as our first step, `group` would not attempt to \"average\" the categorical columns in `nba`. (It is impossible to average two strings like \"Atlanta Hawks\" and \"Boston Celtics\".) It performs arithmetic only on numerical columns and leaves the rest blank."
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" SALARY_mean | \n",
"
\n",
" \n",
" POSITION | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" C | \n",
" 6.082913 | \n",
"
\n",
" \n",
" PF | \n",
" 4.951344 | \n",
"
\n",
" \n",
" PG | \n",
" 5.165487 | \n",
"
\n",
" \n",
" SF | \n",
" 5.532675 | \n",
"
\n",
" \n",
" SG | \n",
" 3.988195 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" SALARY_mean\n",
"POSITION \n",
"C 6.082913\n",
"PF 4.951344\n",
"PG 5.165487\n",
"SF 5.532675\n",
"SG 3.988195"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba_mean = nba.groupby('POSITION').mean()\n",
"\n",
"nba_mean\n",
"\n",
"nba_mean = nba.groupby([\"POSITION\"]).agg(\n",
" SALARY_mean=pd.NamedAgg(column=\"SALARY\", aggfunc=\"mean\")\n",
")\n",
"\n",
"nba_mean"
]
}
],
"metadata": {
"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": 1
}