{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"tags": [
"remove_input"
]
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"path_data = '../../../../data/'\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": [
"### Joining Tables by Columns ###\n",
"Often, data about the same individuals is maintained in more than one table. For example, one university office might have data about each student's time to completion of degree, while another has data about the student's tuition and financial aid.\n",
"\n",
"To understand the *students'* experience, it may be helpful to put the two datasets together. If the data are in two tables, each with one row per student, then we would want to put the columns together, making sure to match the rows so that each student's information remains on a single row.\n",
"\n",
"Let us do this in the context of a simple example, and then use the method with a larger dataset.\n",
"\n",
"[Pandas Merge, Join, Concatenate](https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The table `cones` is one we have encountered earlier. Now suppose each flavor of ice cream comes with a rating that is in a separate table."
]
},
{
"cell_type": "code",
"execution_count": 12,
"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",
" vanilla | \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.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price\n",
"0 strawberry 3.55\n",
"1 vanilla 4.75\n",
"2 chocolate 6.55\n",
"3 strawberry 5.25\n",
"4 chocolate 5.75"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones = pd.DataFrame(\n",
" {'Flavor':np.array(['strawberry', 'vanilla', 'chocolate', 'strawberry', 'chocolate']),\n",
" 'Price':np.array([3.55, 4.75, 6.55, 5.25, 5.75])}\n",
")\n",
"cones"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Kind | \n",
" Stars | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" 2.5 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" 3.5 | \n",
"
\n",
" \n",
" 2 | \n",
" vanilla | \n",
" 4.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Kind Stars\n",
"0 strawberry 2.5\n",
"1 chocolate 3.5\n",
"2 vanilla 4.0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ratings = pd.DataFrame(\n",
" {'Kind':np.array(['strawberry', 'chocolate', 'vanilla']),\n",
" 'Stars':np.array([2.5, 3.5, 4])}\n",
")\n",
"ratings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each of the tables has a column that contains ice cream flavors: `cones` has the column `Flavor`, and `ratings` has the column `Kind`. The entries in these columns can be used to link the two tables.\n",
"\n",
"The method `join` creates a new table in which each cone in the `cones` table is augmented with the Stars information in the `ratings` table. For each cone in `cones`, `join` finds a row in `ratings` whose `Kind` matches the cone's `Flavor`. \n",
"\n",
"In this instance we are going to `join` two df's by [`joining key columns on an index`](https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html#joining-key-columns-on-an-index). To implement a `join` on an index we must create the index we wish to use in the second df, then we have to tell `join` to use those columns for matching.\n",
"\n",
"[Pandas - key columns](https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html#joining-key-columns-on-an-index)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Price | \n",
" Kind | \n",
" Stars | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" 3.55 | \n",
" strawberry | \n",
" 2.5 | \n",
"
\n",
" \n",
" 1 | \n",
" vanilla | \n",
" 4.75 | \n",
" vanilla | \n",
" 4.0 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" 6.55 | \n",
" chocolate | \n",
" 3.5 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" 5.25 | \n",
" strawberry | \n",
" 2.5 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.75 | \n",
" chocolate | \n",
" 3.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price Kind Stars\n",
"0 strawberry 3.55 strawberry 2.5\n",
"1 vanilla 4.75 vanilla 4.0\n",
"2 chocolate 6.55 chocolate 3.5\n",
"3 strawberry 5.25 strawberry 2.5\n",
"4 chocolate 5.75 chocolate 3.5"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones = pd.DataFrame(\n",
" {'Flavor':np.array(['strawberry', 'vanilla', 'chocolate', 'strawberry', 'chocolate']),\n",
" 'Price':np.array([3.55, 4.75, 6.55, 5.25, 5.75])}\n",
")\n",
"\n",
"ratings = pd.DataFrame(\n",
" {'Kind':np.array(['strawberry', 'chocolate', 'vanilla']),\n",
" 'Stars':np.array([2.5, 3.5, 4])},\n",
" index=np.array(['strawberry', 'chocolate', 'vanilla']))\n",
"\n",
"rates = cones.join(ratings, on='Flavor')\n",
"\n",
"rates"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This will create a df which includes the 'Kind' column i.e. we are repeating the flavours. To display only the columns in which we are interested -"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Price | \n",
" Stars | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" 3.55 | \n",
" 2.5 | \n",
"
\n",
" \n",
" 1 | \n",
" vanilla | \n",
" 4.75 | \n",
" 4.0 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" 6.55 | \n",
" 3.5 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" 5.25 | \n",
" 2.5 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.75 | \n",
" 3.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price Stars\n",
"0 strawberry 3.55 2.5\n",
"1 vanilla 4.75 4.0\n",
"2 chocolate 6.55 3.5\n",
"3 strawberry 5.25 2.5\n",
"4 chocolate 5.75 3.5"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#rated = rates[['Flavor', 'Price', 'Stars']].sort_values(by=(['Flavor']))\n",
"\n",
"#or\n",
"\n",
"rated = rates.drop(columns=['Kind'])\n",
"\n",
"rated"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each cone now has not only its price but also the rating of its flavor.\n",
"\n",
"In general, a call to `join` that augments a table (say `table1`) with information from another table (say `table2`) looks like this:\n",
"\n",
" table1.join(table2, table1_column_for_joining)\n",
"\n",
"The new table `rated` allows us to work out the price per star, which you can think of as an informal measure of value. Low values are good – they mean that you are paying less for each rating star."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Price | \n",
" Stars | \n",
" $/Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" vanilla | \n",
" 4.75 | \n",
" 4.0 | \n",
" 1.187500 | \n",
"
\n",
" \n",
" 0 | \n",
" strawberry | \n",
" 3.55 | \n",
" 2.5 | \n",
" 1.420000 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.75 | \n",
" 3.5 | \n",
" 1.642857 | \n",
"
\n",
" \n",
" 2 | \n",
" chocolate | \n",
" 6.55 | \n",
" 3.5 | \n",
" 1.871429 | \n",
"
\n",
" \n",
" 3 | \n",
" strawberry | \n",
" 5.25 | \n",
" 2.5 | \n",
" 2.100000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price Stars $/Price\n",
"1 vanilla 4.75 4.0 1.187500\n",
"0 strawberry 3.55 2.5 1.420000\n",
"4 chocolate 5.75 3.5 1.642857\n",
"2 chocolate 6.55 3.5 1.871429\n",
"3 strawberry 5.25 2.5 2.100000"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rated['$/Price'] = rated['Price'] / rated['Stars']\n",
"\n",
"rated.sort_values('$/Price')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Though strawberry has the lowest rating among the three flavors, the less expensive strawberry cone does well on this measure because it doesn't cost a lot per star."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Side note.** Does the order we list the two tables matter? Let's try it. As you see it, this changes the order that the columns appear in, and can potentially changes the order of the rows, but it doesn't make any fundamental difference."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Kind | \n",
" Stars | \n",
" Price | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" strawberry | \n",
" 2.5 | \n",
" 3.55 | \n",
"
\n",
" \n",
" 0 | \n",
" strawberry | \n",
" 2.5 | \n",
" 5.25 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" 3.5 | \n",
" 6.55 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" 3.5 | \n",
" 5.75 | \n",
"
\n",
" \n",
" 2 | \n",
" vanilla | \n",
" 4.0 | \n",
" 4.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Kind Stars Price\n",
"0 strawberry 2.5 3.55\n",
"0 strawberry 2.5 5.25\n",
"1 chocolate 3.5 6.55\n",
"1 chocolate 3.5 5.75\n",
"2 vanilla 4.0 4.75"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones1 = pd.DataFrame(\n",
" {'Flavor':np.array(['strawberry', 'vanilla', 'chocolate', 'strawberry', 'chocolate']),\n",
" 'Price':np.array([3.55, 4.75, 6.55, 5.25, 5.75])},\n",
" index=np.array(['strawberry', 'vanilla', 'chocolate', 'strawberry', 'chocolate'])\n",
")\n",
"\n",
"ratings = pd.DataFrame(\n",
" {'Kind':np.array(['strawberry', 'chocolate', 'vanilla']),\n",
" 'Stars':np.array([2.5, 3.5, 4])})\n",
"\n",
"rates = ratings.join(cones1, on='Kind')\n",
"\n",
"rates = rates.drop(columns=['Flavor'])\n",
"\n",
"rates"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also note that the join will only contain information about items that appear in both tables. Let's see an example. Suppose there is a table of reviews of some ice cream cones, and we have found the average or `mean` of reviews for each flavor."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Stars | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" vanilla | \n",
" 5 | \n",
"
\n",
" \n",
" 1 | \n",
" chocolate | \n",
" 3 | \n",
"
\n",
" \n",
" 2 | \n",
" vanilla | \n",
" 5 | \n",
"
\n",
" \n",
" 3 | \n",
" chocolate | \n",
" 4 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Stars\n",
"0 vanilla 5\n",
"1 chocolate 3\n",
"2 vanilla 5\n",
"3 chocolate 4"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reviews = pd.DataFrame(\n",
" {'Flavor':np.array(['vanilla', 'chocolate', 'vanilla', 'chocolate']),\n",
" 'Stars':np.array([5, 3, 5, 4])}\n",
")\n",
"reviews"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Stars average | \n",
"
\n",
" \n",
" Flavor | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | \n",
" 3.5 | \n",
"
\n",
" \n",
" vanilla | \n",
" 5.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Stars average\n",
"Flavor \n",
"chocolate 3.5\n",
"vanilla 5.0"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average_review = reviews.groupby('Flavor').mean()\n",
"\n",
"average_review = average_review.rename(columns={'Stars':'Stars average'})\n",
"\n",
"average_review"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can join `cones` and `average_review` by providing the labels of the columns by which to join."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Flavor | \n",
" Price | \n",
" Stars average | \n",
"
\n",
" \n",
" \n",
" \n",
" 2 | \n",
" chocolate | \n",
" 6.55 | \n",
" 3.5 | \n",
"
\n",
" \n",
" 4 | \n",
" chocolate | \n",
" 5.75 | \n",
" 3.5 | \n",
"
\n",
" \n",
" 1 | \n",
" vanilla | \n",
" 4.75 | \n",
" 5.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Flavor Price Stars average\n",
"2 chocolate 6.55 3.5\n",
"4 chocolate 5.75 3.5\n",
"1 vanilla 4.75 5.0"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reviewers = cones.join(average_review, on='Flavor')\n",
"\n",
"reviewers = reviewers.rename(columns={'Stars':'Stars average'})\n",
"\n",
"reviewers = reviewers.dropna()\n",
"\n",
"reviewers.sort_values(['Stars average'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how the strawberry cones have disappeared. None of the reviews are for strawberry cones, so there is nothing to which the `strawberry` rows can be joined. This might be a problem, or it might not be - that depends on the analysis we are trying to perform with the joined table."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}