<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Intro Session
Step1:  Notebook
The Jupyter Notebook is a literate programming tool that lets you combine Python code, Markdown text, and other commands. Each notebook is composed of cells, and cells can either be code (e.g. Python) or Markdown.
In addition to Python code, the notebook can run so-called magic functions. These begin with a percent % symbol and can be used to do common tasks like listing files with %ls
Step2:  Both the magic functions and the python ones support tab-completion
Step3:  Data
Step4:  Task 1
Step5:  Visualize
Step6:  Task 2
Step7:  loc[&lt;col&gt;, 'row'] allows assignment with =
Step8:  Task 3
Step9:  Task 3 - Stretch goal
Step10:  To get each country as a series, we create a 2-level index
Step11:  The data table now indexes by country and year. 
See how they appear on the second header row, and the data is grouped by country?
Step12:  Visualization code is almost the same, but we tell Pandas how to unstack the data into multiple series
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
Explanation: Intro Session: Motivating reproducibility
This Jupyter Notebook file contains Python code you can use to complete the exercises from the Intro
session. To see the output simply click Cell->Run All above
Pandas:
pandas is a data analysis package for Python, which provides similar functionality to R. Typically, this is imported as pd for the convenience of typing fewer characters.
End of explanation
%ls
Explanation: Notebook
The Jupyter Notebook is a literate programming tool that lets you combine Python code, Markdown text, and other commands. Each notebook is composed of cells, and cells can either be code (e.g. Python) or Markdown.
In addition to Python code, the notebook can run so-called magic functions. These begin with a percent % symbol and can be used to do common tasks like listing files with %ls
End of explanation
%ls rr-intro-data-v0.2/intro/data/
Explanation: Both the magic functions and the python ones support tab-completion
End of explanation
gap_5060 = pd.read_csv('rr-intro-data-v0.2/intro/data/gapminder-5060.csv')
Explanation: Data:
In the pandas package, the read_csv function is used to read the data into a pandas Data Frame. Note that the argument provided to this function is the complete path that leads to the dataset from your current 
working directory (where this Notebook is located). Also note that this is provided 
as a character string, hence in quotation marks.
Read the Gapminder 1950-1960 data in from CSV file
End of explanation
gap_5060_CA = gap_5060.loc[gap_5060['country'] == 'Canada']
Explanation: Task 1: Visualize life expectancy over time for Canada in the 1950s and 1960s using a line plot.
Filter the data for Canada only:
End of explanation
%matplotlib inline
gap_5060_CA.plot(kind='line', x='year', y='lifeExp')
pass
Explanation: Visualize:
Pandas uses matplotlib by default, and a very common practice is to generate plots inline - meaning within the notebook and not as separate files. A magic function configures this, and you'll often just put it at the top of your notebook with import pandas as pd
End of explanation
gap_5060.loc[(gap_5060['country'] == 'Canada') & (gap_5060['year'] == 1957)]
Explanation: Task 2: Something is clearly wrong with this plot!
Turns out there's a data error in the data file: life expectancy for Canada in the year 1957 is coded as 999999, it should actually be 69.96. Make this correction.
loc and ['column'] to index into the Data Frame
End of explanation
gap_5060.loc[(gap_5060['country'] == 'Canada') & (gap_5060['year'] == 1957), 'lifeExp'] = 69.96
gap_5060.loc[(gap_5060['country'] == 'Canada') & (gap_5060['year'] == 1957)]
Explanation: loc[&lt;col&gt;, 'row'] allows assignment with =
End of explanation
gap_5060_CA = gap_5060.loc[gap_5060['country'] == 'Canada']
gap_5060_CA.plot(kind='line', x='year', y='lifeExp')
pass
Explanation: Task 3: Visualize life expectancy over time for Canada again, with the corrected data.
Exact same code as before, but note that the contents of gap_5060 are different as it 
has been updated in the previous task.
End of explanation
loc = gap_5060['country'].isin(['Canada','United States','Mexico'])
us_mexico_ca = gap_5060.loc[loc]
Explanation: Task 3 - Stretch goal: Add lines for Mexico and United States.
.isin() for logical operator testing if a country's name is in the list provided
End of explanation
indexed_by_country = us_mexico_ca.set_index(['country','year'])
Explanation: To get each country as a series, we create a 2-level index
End of explanation
indexed_by_country
Explanation: The data table now indexes by country and year. 
See how they appear on the second header row, and the data is grouped by country?
End of explanation
indexed_by_country.unstack(level='country').plot(kind='line',y='lifeExp')
pass
Explanation: Visualization code is almost the same, but we tell Pandas how to unstack the data into multiple series
End of explanation
<END_TASK>