```python
! pip install qeds
import pandas as pd
import numpy as np
```

    Requirement already satisfied: qeds in c:\users\asus\anaconda3\lib\site-packages (0.6.2)
    Requirement already satisfied: matplotlib in c:\users\asus\anaconda3\lib\site-packages (from qeds) (3.1.1)
    Requirement already satisfied: pandas in c:\users\asus\anaconda3\lib\site-packages (from qeds) (0.25.1)
    Requirement already satisfied: pyarrow in c:\users\asus\anaconda3\lib\site-packages (from qeds) (0.16.0)
    Requirement already satisfied: openpyxl in c:\users\asus\anaconda3\lib\site-packages (from qeds) (3.0.0)
    Requirement already satisfied: plotly in c:\users\asus\anaconda3\lib\site-packages (from qeds) (4.5.4)
    Requirement already satisfied: pandas-datareader in c:\users\asus\anaconda3\lib\site-packages (from qeds) (0.8.1)
    Requirement already satisfied: seaborn in c:\users\asus\anaconda3\lib\site-packages (from qeds) (0.9.0)
    Requirement already satisfied: scipy in c:\users\asus\anaconda3\lib\site-packages (from qeds) (1.3.1)
    Requirement already satisfied: scikit-learn in c:\users\asus\anaconda3\lib\site-packages (from qeds) (0.21.3)
    Requirement already satisfied: statsmodels in c:\users\asus\anaconda3\lib\site-packages (from qeds) (0.10.1)
    Requirement already satisfied: numpy in c:\users\asus\anaconda3\lib\site-packages (from qeds) (1.16.5)
    Requirement already satisfied: requests in c:\users\asus\anaconda3\lib\site-packages (from qeds) (2.22.0)
    Requirement already satisfied: quantecon in c:\users\asus\anaconda3\lib\site-packages (from qeds) (0.4.6)
    Requirement already satisfied: quandl in c:\users\asus\anaconda3\lib\site-packages (from qeds) (3.5.0)
    Requirement already satisfied: cycler>=0.10 in c:\users\asus\anaconda3\lib\site-packages (from matplotlib->qeds) (0.10.0)
    Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\asus\anaconda3\lib\site-packages (from matplotlib->qeds) (1.1.0)
    Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\users\asus\anaconda3\lib\site-packages (from matplotlib->qeds) (2.4.2)
    Requirement already satisfied: python-dateutil>=2.1 in c:\users\asus\anaconda3\lib\site-packages (from matplotlib->qeds) (2.8.0)
    Requirement already satisfied: pytz>=2017.2 in c:\users\asus\anaconda3\lib\site-packages (from pandas->qeds) (2019.3)
    Requirement already satisfied: six>=1.0.0 in c:\users\asus\anaconda3\lib\site-packages (from pyarrow->qeds) (1.12.0)
    Requirement already satisfied: et-xmlfile in c:\users\asus\anaconda3\lib\site-packages (from openpyxl->qeds) (1.0.1)
    Requirement already satisfied: jdcal in c:\users\asus\anaconda3\lib\site-packages (from openpyxl->qeds) (1.4.1)
    Requirement already satisfied: retrying>=1.3.3 in c:\users\asus\anaconda3\lib\site-packages (from plotly->qeds) (1.3.3)
    Requirement already satisfied: lxml in c:\users\asus\anaconda3\lib\site-packages (from pandas-datareader->qeds) (4.4.1)
    Requirement already satisfied: joblib>=0.11 in c:\users\asus\anaconda3\lib\site-packages (from scikit-learn->qeds) (0.13.2)
    Requirement already satisfied: patsy>=0.4.0 in c:\users\asus\anaconda3\lib\site-packages (from statsmodels->qeds) (0.5.1)
    Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\asus\anaconda3\lib\site-packages (from requests->qeds) (3.0.4)
    Requirement already satisfied: certifi>=2017.4.17 in c:\users\asus\anaconda3\lib\site-packages (from requests->qeds) (2019.9.11)
    Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\asus\anaconda3\lib\site-packages (from requests->qeds) (1.24.2)
    Requirement already satisfied: idna<2.9,>=2.5 in c:\users\asus\anaconda3\lib\site-packages (from requests->qeds) (2.8)
    Requirement already satisfied: numba>=0.38 in c:\users\asus\anaconda3\lib\site-packages (from quantecon->qeds) (0.45.1)
    Requirement already satisfied: sympy in c:\users\asus\anaconda3\lib\site-packages (from quantecon->qeds) (1.4)
    Requirement already satisfied: inflection>=0.3.1 in c:\users\asus\anaconda3\lib\site-packages (from quandl->qeds) (0.3.1)
    Requirement already satisfied: more-itertools in c:\users\asus\anaconda3\lib\site-packages (from quandl->qeds) (7.2.0)
    Requirement already satisfied: setuptools in c:\users\asus\anaconda3\lib\site-packages (from kiwisolver>=1.0.1->matplotlib->qeds) (41.4.0)
    Requirement already satisfied: llvmlite>=0.29.0dev0 in c:\users\asus\anaconda3\lib\site-packages (from numba>=0.38->quantecon->qeds) (0.29.0)
    Requirement already satisfied: mpmath>=0.19 in c:\users\asus\anaconda3\lib\site-packages (from sympy->quantecon->qeds) (1.1.0)

# The Index - Exercises

**For these exercises we load data on GDP components collected from the World Bank’s World Development Indicators Dataset.**

## Exercise 1

**For the purpose of this exercise we extract smaller DataFrames.**

```python
url = "https://datascience.quantecon.org/assets/data/wdi_data.csv"
df = pd.read_csv(url)
#df.head()
```

```python
df_small = df.head(5)
#df_small
```

```python
df_tiny = df.iloc[[0, 3, 2, 4], :]
#df_tiny
```

```python
im_ex = df_small[["Imports", "Exports"]]
im_ex_copy = im_ex.copy()
#im_ex_copy
```

1. **Create a new DataFrame as follows: im_ex_tiny = df_tiny + im_ex. Analyze the outcome.**

```python
im_ex_tiny = df_tiny + im_ex
im_ex_tiny
```

      Consumption
      Exports
      GDP
      GovExpend
      Imports
      country
      year

      0
      NaN
      1.165661
      NaN
      NaN
      1.200063
      NaN
      NaN

      1
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      2
      NaN
      1.137718
      NaN
      NaN
      1.151585
      NaN
      NaN

      3
      NaN
      1.100646
      NaN
      NaN
      1.144688
      NaN
      NaN

      4
      NaN
      1.036081
      NaN
      NaN
      1.117272
      NaN
      NaN

2. **What happens when you apply the mean method to im_ex_tiny? In particular, what happens to columns that have missing data?**

    - HINT: also looking at the output of the sum method might help

```python
im_ex_tiny.mean()

#we get missing values for the columns where all values are missing (NaN)
#the mean is computed for the other columns even though there are missing values
```

    Consumption         NaN
    Exports        1.110027
    GDP                 NaN
    GovExpend           NaN
    Imports        1.153402
    country             NaN
    year                NaN
    dtype: float64

```python
im_ex_tiny.sum()

#we get 0s for the columns where all values are missing (NaN)
#the sum is computed for the other columns (even though there are missing values)
```

    Consumption    0.000000
    Exports        4.440106
    GDP            0.000000
    GovExpend      0.000000
    Imports        4.613608
    country        0.000000
    year           0.000000
    dtype: float64

## Exercises 2-6

**We create a new DataFrame to use throughout the exercises 2-6. Now we set "country" and "year" as indices.**

```python
wdi = df.set_index(["country", "year"])
wdi.head()
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      Canada
      2017
      0.372665
      1.095475
      0.582831
      0.600031
      1.868164

      2016
      0.364899
      1.058426
      0.576394
      0.575775
      1.814016

      2015
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

      2014
      0.353485
      1.011988
      0.550323
      0.572344
      1.782252

      2013
      0.351541
      0.986400
      0.518040
      0.558636
      1.732714

## Exercise 2

**For each of the examples below do the following and write your answers:**

    - Determine which of the rules above applies.
    - Identify the type of the returned value.
    - Explain why the slicing operation returned the data it did.

```python
wdi.loc[["United States", "Canada"]]

#row slicing
#all rows where the outer-most index is either "United States" or "Canada"
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      Canada
      2017
      0.372665
      1.095475
      0.582831
      0.600031
      1.868164

      2016
      0.364899
      1.058426
      0.576394
      0.575775
      1.814016

      2015
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

      2014
      0.353485
      1.011988
      0.550323
      0.572344
      1.782252

      2013
      0.351541
      0.986400
      0.518040
      0.558636
      1.732714

      2012
      0.354342
      0.961226
      0.505969
      0.547756
      1.693428

      2011
      0.351887
      0.943145
      0.492349
      0.528227
      1.664240

      2010
      0.347332
      0.921952
      0.469949
      0.500341
      1.613543

      2009
      0.339686
      0.890078
      0.440692
      0.439796
      1.565291

      2008
      0.330766
      0.889602
      0.506350
      0.502281
      1.612862

      2007
      0.318777
      0.864012
      0.530453
      0.498002
      1.596876

      2006
      0.311382
      0.827643
      0.524461
      0.470931
      1.564608

      2005
      0.303043
      0.794390
      0.519950
      0.447222
      1.524608

      2004
      0.299854
      0.764357
      0.508657
      0.416754
      1.477317

      2003
      0.294335
      0.741796
      0.481993
      0.384199
      1.433089

      2002
      0.286094
      0.721974
      0.490465
      0.368615
      1.407725

      2001
      0.279767
      0.694230
      0.484696
      0.362023
      1.366590

      2000
      0.270553
      0.677713
      0.499526
      0.380823
      1.342805

      United States
      2017
      2.405743
      12.019266
      2.287071
      3.069954
      17.348627

      2016
      2.407981
      11.722133
      2.219937
      2.936004
      16.972348

      2015
      2.373130
      11.409800
      2.222228
      2.881337
      16.710459

      2014
      2.334071
      11.000619
      2.209555
      2.732228
      16.242526

      2013
      2.353381
      10.687214
      2.118639
      2.600198
      15.853796

      2012
      2.398873
      10.534042
      2.045509
      2.560677
      15.567038

      2011
      2.434378
      10.378060
      1.978083
      2.493194
      15.224555

      2010
      2.510143
      10.185836
      1.846280
      2.360183
      14.992053

      2009
      2.507390
      10.010687
      1.646432
      2.086299
      14.617299

      2008
      2.407771
      10.137847
      1.797347
      2.400349
      14.997756

      2007
      2.351987
      10.159387
      1.701096
      2.455016
      15.018268

      2006
      2.314957
      9.938503
      1.564920
      2.395189
      14.741688

      2005
      2.287022
      9.643098
      1.431205
      2.246246
      14.332500

      2004
      2.267999
      9.311431
      1.335978
      2.108585
      13.846058

      2003
      2.233519
      8.974708
      1.218199
      1.892825
      13.339312

      2002
      2.193188
      8.698306
      1.192180
      1.804105
      12.968263

      2001
      2.112038
      8.480461
      1.213253
      1.740797
      12.746262

      2000
      2.040500
      8.272097
      1.287739
      1.790995
      12.620268

```python
wdi.loc[(["United States", "Canada"], [2010, 2011, 2012]), :]

#row slicing
#all rows where the outer-most index is either "United States or "Canada" AND
#where the second level index is either 2010 or 2011 or 2012
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      Canada
      2012
      0.354342
      0.961226
      0.505969
      0.547756
      1.693428

      2011
      0.351887
      0.943145
      0.492349
      0.528227
      1.664240

      2010
      0.347332
      0.921952
      0.469949
      0.500341
      1.613543

      United States
      2012
      2.398873
      10.534042
      2.045509
      2.560677
      15.567038

      2011
      2.434378
      10.378060
      1.978083
      2.493194
      15.224555

      2010
      2.510143
      10.185836
      1.846280
      2.360183
      14.992053

```python
wdi.loc["United States"]

#row slicing
#all rows where the outer most index value is equal to United States
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      year

      2017
      2.405743
      12.019266
      2.287071
      3.069954
      17.348627

      2016
      2.407981
      11.722133
      2.219937
      2.936004
      16.972348

      2015
      2.373130
      11.409800
      2.222228
      2.881337
      16.710459

      2014
      2.334071
      11.000619
      2.209555
      2.732228
      16.242526

      2013
      2.353381
      10.687214
      2.118639
      2.600198
      15.853796

      2012
      2.398873
      10.534042
      2.045509
      2.560677
      15.567038

      2011
      2.434378
      10.378060
      1.978083
      2.493194
      15.224555

      2010
      2.510143
      10.185836
      1.846280
      2.360183
      14.992053

      2009
      2.507390
      10.010687
      1.646432
      2.086299
      14.617299

      2008
      2.407771
      10.137847
      1.797347
      2.400349
      14.997756

      2007
      2.351987
      10.159387
      1.701096
      2.455016
      15.018268

      2006
      2.314957
      9.938503
      1.564920
      2.395189
      14.741688

      2005
      2.287022
      9.643098
      1.431205
      2.246246
      14.332500

      2004
      2.267999
      9.311431
      1.335978
      2.108585
      13.846058

      2003
      2.233519
      8.974708
      1.218199
      1.892825
      13.339312

      2002
      2.193188
      8.698306
      1.192180
      1.804105
      12.968263

      2001
      2.112038
      8.480461
      1.213253
      1.740797
      12.746262

      2000
      2.040500
      8.272097
      1.287739
      1.790995
      12.620268

```python
wdi.loc[("United States", 2010), ["GDP", "Exports"]]

#row + column slicing
#we get the values for the GDP and Exports columns in the United States in 2010
#however, this is not presented as expected (the column names appear as indices)
# Tuple used to select united states on year 2010. Then, only GDP and Exports columns are extracted
```

    GDP        14.992053
    Exports     1.846280
    Name: (United States, 2010), dtype: float64

```python
wdi.loc[("United States", 2010)]

#row slicing
#we get the values for all columns in the United States in 2010
#however, this is not presented as expected (the column names appear as indices)
```

    GovExpend       2.510143
    Consumption    10.185836
    Exports         1.846280
    Imports         2.360183
    GDP            14.992053
    Name: (United States, 2010), dtype: float64

```python
wdi.loc[[("United States", 2010), ("Canada", 2015)]]

#row slicing
#all rows where the the two hierarchical indices are either ("United States", 2010) or ("Canada", 2015)
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      United States
      2010
      2.510143
      10.185836
      1.846280
      2.360183
      14.992053

      Canada
      2015
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

```python
wdi.loc[["United States", "Canada"], "GDP"]

#row + column slicing
#all rows for United States or Canada only for the column GDP
```

    country        year
    Canada         2017     1.868164
                   2016     1.814016
                   2015     1.794270
                   2014     1.782252
                   2013     1.732714
                   2012     1.693428
                   2011     1.664240
                   2010     1.613543
                   2009     1.565291
                   2008     1.612862
                   2007     1.596876
                   2006     1.564608
                   2005     1.524608
                   2004     1.477317
                   2003     1.433089
                   2002     1.407725
                   2001     1.366590
                   2000     1.342805
    United States  2017    17.348627
                   2016    16.972348
                   2015    16.710459
                   2014    16.242526
                   2013    15.853796
                   2012    15.567038
                   2011    15.224555
                   2010    14.992053
                   2009    14.617299
                   2008    14.997756
                   2007    15.018268
                   2006    14.741688
                   2005    14.332500
                   2004    13.846058
                   2003    13.339312
                   2002    12.968263
                   2001    12.746262
                   2000    12.620268
    Name: GDP, dtype: float64

```python
wdi.loc["United States", "GDP"]

#row + column slicing
#all rows for the US for column GDP
```

    year
    2017    17.348627
    2016    16.972348
    2015    16.710459
    2014    16.242526
    2013    15.853796
    2012    15.567038
    2011    15.224555
    2010    14.992053
    2009    14.617299
    2008    14.997756
    2007    15.018268
    2006    14.741688
    2005    14.332500
    2004    13.846058
    2003    13.339312
    2002    12.968263
    2001    12.746262
    2000    12.620268
    Name: GDP, dtype: float64

## Exercise 3

1. **Try setting my_df to some subset of the rows in wdi (use one of the .loc variations above).**

```python
#below we take a few examples (we comment the ones not used in the analysis below)

my_df = wdi.loc[[("United States", 2010), ("Canada", 2015)]]
#my_df = wdi.loc["United States"]
#my_df = wdi.loc[("United States", 2010)]
#my_df = wdi.loc["United States", "GDP"]
#my_df = wdi.loc[["United States", "Canada"]]

my_df
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      United States
      2010
      2.510143
      10.185836
      1.846280
      2.360183
      14.992053

      Canada
      2015
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

2. **Then see what happens when you do wdi / my_df or my_df ** wdi.**

```python
#we create a variable that does wdi/my_df and we analyze the outcome
x = wdi / my_df
x

#since it is not clear at a first look, it's better if we extract the subsets used before to see what happens (uncomment to check):
    #x_df = x.loc[[("United States", 2010), ("Canada", 2015)]]
    #x_df = x.loc["United States"]
    #x_df = x.loc[("United States", 2010)]
    #x_df = x.loc["United States", "GDP"]
    #x_df = x.loc[["United States", "Canada"]]
    #x_df
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      Canada
      2000
      NaN
      NaN
      NaN
      NaN
      NaN

      2001
      NaN
      NaN
      NaN
      NaN
      NaN

      2002
      NaN
      NaN
      NaN
      NaN
      NaN

      2003
      NaN
      NaN
      NaN
      NaN
      NaN

      2004
      NaN
      NaN
      NaN
      NaN
      NaN

      ...
      ...
      ...
      ...
      ...
      ...
      ...

      United States
      2013
      NaN
      NaN
      NaN
      NaN
      NaN

      2014
      NaN
      NaN
      NaN
      NaN
      NaN

      2015
      NaN
      NaN
      NaN
      NaN
      NaN

      2016
      NaN
      NaN
      NaN
      NaN
      NaN

      2017
      NaN
      NaN
      NaN
      NaN
      NaN

72 rows × 5 columns

```python
#we create a variable that does wdi**my_df and we analyze the outcome
y = wdi ** my_df
y

#since it is not clear at a first look, it's better if we extract the subsets used before to see what happens (uncomment to check):
    #y_df = y.loc[[("United States", 2010), ("Canada", 2015)]]
    #y_df = y.loc["United States"]
    #y_df = y.loc[("United States", 2010)]
    #y_df = y.loc["United States", "GDP"]
    #y_df = y.loc[["United States", "Canada"]]
    #y_df
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      Canada
      2000
      NaN
      NaN
      NaN
      NaN
      NaN

      2001
      NaN
      NaN
      NaN
      NaN
      NaN

      2002
      NaN
      NaN
      NaN
      NaN
      NaN

      2003
      NaN
      NaN
      NaN
      NaN
      NaN

      2004
      NaN
      NaN
      NaN
      NaN
      NaN

      ...
      ...
      ...
      ...
      ...
      ...
      ...

      United States
      2013
      NaN
      NaN
      NaN
      NaN
      NaN

      2014
      NaN
      NaN
      NaN
      NaN
      NaN

      2015
      NaN
      NaN
      NaN
      NaN
      NaN

      2016
      NaN
      NaN
      NaN
      NaN
      NaN

      2017
      NaN
      NaN
      NaN
      NaN
      NaN

72 rows × 5 columns

3. **Try changing the subset of rows in my_df and repeat until you understand what is happening.**

```python
#conclusion: the operation applies only to the subset my_df and not to the entire DataFrame wdi
```

## Exercise 4

1. **Below, we create wdi2, which is the same as df4 except that the levels of the index are swapped.**

```python
wdi2 = df.set_index(["year", "country"])
wdi2.head()
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      year
      country

      2017
      Canada
      0.372665
      1.095475
      0.582831
      0.600031
      1.868164

      2016
      Canada
      0.364899
      1.058426
      0.576394
      0.575775
      1.814016

      2015
      Canada
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

      2014
      Canada
      0.353485
      1.011988
      0.550323
      0.572344
      1.782252

      2013
      Canada
      0.351541
      0.986400
      0.518040
      0.558636
      1.732714

2. **In the cells after df6 is defined, we have commented out a few of the slicing examples from the previous exercise. For each of these examples, use pd.IndexSlice to extract the same data from df6.**

    - HINT: You will need to swap the order of the row slicing arguments within the pd.IndexSlice.

```python
#the equivalent of wdi.loc["United States"] is below:

#we create a shortcut for pd.IndexSlice to use in what follows
idx = pd.IndexSlice

wdi.loc[idx["United States",:],:]
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      United States
      2017
      2.405743
      12.019266
      2.287071
      3.069954
      17.348627

      2016
      2.407981
      11.722133
      2.219937
      2.936004
      16.972348

      2015
      2.373130
      11.409800
      2.222228
      2.881337
      16.710459

      2014
      2.334071
      11.000619
      2.209555
      2.732228
      16.242526

      2013
      2.353381
      10.687214
      2.118639
      2.600198
      15.853796

      2012
      2.398873
      10.534042
      2.045509
      2.560677
      15.567038

      2011
      2.434378
      10.378060
      1.978083
      2.493194
      15.224555

      2010
      2.510143
      10.185836
      1.846280
      2.360183
      14.992053

      2009
      2.507390
      10.010687
      1.646432
      2.086299
      14.617299

      2008
      2.407771
      10.137847
      1.797347
      2.400349
      14.997756

      2007
      2.351987
      10.159387
      1.701096
      2.455016
      15.018268

      2006
      2.314957
      9.938503
      1.564920
      2.395189
      14.741688

      2005
      2.287022
      9.643098
      1.431205
      2.246246
      14.332500

      2004
      2.267999
      9.311431
      1.335978
      2.108585
      13.846058

      2003
      2.233519
      8.974708
      1.218199
      1.892825
      13.339312

      2002
      2.193188
      8.698306
      1.192180
      1.804105
      12.968263

      2001
      2.112038
      8.480461
      1.213253
      1.740797
      12.746262

      2000
      2.040500
      8.272097
      1.287739
      1.790995
      12.620268

```python
#the equivalent of wdi.loc[(["United States", "Canada"], [2010, 2011, 2012]), :] is below:

wdi.loc[idx[(["United States", "Canada"],[2010, 2011, 2012])], :]
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country
      year

      Canada
      2012
      0.354342
      0.961226
      0.505969
      0.547756
      1.693428

      2011
      0.351887
      0.943145
      0.492349
      0.528227
      1.664240

      2010
      0.347332
      0.921952
      0.469949
      0.500341
      1.613543

      United States
      2012
      2.398873
      10.534042
      2.045509
      2.560677
      15.567038

      2011
      2.434378
      10.378060
      1.978083
      2.493194
      15.224555

      2010
      2.510143
      10.185836
      1.846280
      2.360183
      14.992053

```python
#the equivalent of wdi.loc[["United States", "Canada"], "GDP"] is below:

wdi.loc[idx[["United States", "Canada"],:], "GDP"]
```

    country        year
    Canada         2017     1.868164
                   2016     1.814016
                   2015     1.794270
                   2014     1.782252
                   2013     1.732714
                   2012     1.693428
                   2011     1.664240
                   2010     1.613543
                   2009     1.565291
                   2008     1.612862
                   2007     1.596876
                   2006     1.564608
                   2005     1.524608
                   2004     1.477317
                   2003     1.433089
                   2002     1.407725
                   2001     1.366590
                   2000     1.342805
    United States  2017    17.348627
                   2016    16.972348
                   2015    16.710459
                   2014    16.242526
                   2013    15.853796
                   2012    15.567038
                   2011    15.224555
                   2010    14.992053
                   2009    14.617299
                   2008    14.997756
                   2007    15.018268
                   2006    14.741688
                   2005    14.332500
                   2004    13.846058
                   2003    13.339312
                   2002    12.968263
                   2001    12.746262
                   2000    12.620268
    Name: GDP, dtype: float64

## Exercise 5

**For the purpose of this exercise, we create a new DataFrame that swaps the rows and columns of the wdi DataFrame.**

```python
wdiT = wdi.T  # .T means "transpose" or "swap rows and columns"
wdiT
```

      country
      Canada
      ...
      United States

      year
      2017
      2016
      2015
      2014
      2013
      2012
      2011
      2010
      2009
      2008
      ...
      2009
      2008
      2007
      2006
      2005
      2004
      2003
      2002
      2001
      2000

      GovExpend
      0.372665
      0.364899
      0.358303
      0.353485
      0.351541
      0.354342
      0.351887
      0.347332
      0.339686
      0.330766
      ...
      2.507390
      2.407771
      2.351987
      2.314957
      2.287022
      2.267999
      2.233519
      2.193188
      2.112038
      2.040500

      Consumption
      1.095475
      1.058426
      1.035208
      1.011988
      0.986400
      0.961226
      0.943145
      0.921952
      0.890078
      0.889602
      ...
      10.010687
      10.137847
      10.159387
      9.938503
      9.643098
      9.311431
      8.974708
      8.698306
      8.480461
      8.272097

      Exports
      0.582831
      0.576394
      0.568859
      0.550323
      0.518040
      0.505969
      0.492349
      0.469949
      0.440692
      0.506350
      ...
      1.646432
      1.797347
      1.701096
      1.564920
      1.431205
      1.335978
      1.218199
      1.192180
      1.213253
      1.287739

      Imports
      0.600031
      0.575775
      0.575793
      0.572344
      0.558636
      0.547756
      0.528227
      0.500341
      0.439796
      0.502281
      ...
      2.086299
      2.400349
      2.455016
      2.395189
      2.246246
      2.108585
      1.892825
      1.804105
      1.740797
      1.790995

      GDP
      1.868164
      1.814016
      1.794270
      1.782252
      1.732714
      1.693428
      1.664240
      1.613543
      1.565291
      1.612862
      ...
      14.617299
      14.997756
      15.018268
      14.741688
      14.332500
      13.846058
      13.339312
      12.968263
      12.746262
      12.620268

5 rows × 72 columns

**Use pd.IndexSlice to extract all data from wdiT where the year level of the column names (the second level) is one of 2010, 2012, and 2014.**

```python
wdiT.loc[:, idx[:,[2010, 2012, 2014]]]
```

      country
      Canada
      Germany
      United Kingdom
      United States

      year
      2014
      2012
      2010
      2014
      2012
      2010
      2014
      2012
      2010
      2014
      2012
      2010

      GovExpend
      0.353485
      0.354342
      0.347332
      0.685990
      0.666454
      0.653386
      0.538888
      0.528194
      0.521146
      2.334071
      2.398873
      2.510143

      Consumption
      1.011988
      0.961226
      0.921952
      1.999953
      1.967390
      1.915481
      1.675716
      1.612550
      1.598563
      11.000619
      10.534042
      10.185836

      Exports
      0.550323
      0.505969
      0.469949
      1.712270
      1.607455
      1.443735
      0.774022
      0.745484
      0.690824
      2.209555
      2.045509
      1.846280

      Imports
      0.572344
      0.547756
      0.500341
      1.445409
      1.354122
      1.266126
      0.827311
      0.772692
      0.745065
      2.732228
      2.560677
      2.360183

      GDP
      1.782252
      1.693428
      1.613543
      3.654924
      3.559587
      3.417095
      2.657159
      2.529323
      2.452900
      16.242526
      15.567038
      14.992053

## Exercise 6

1. **Move just the `year` level of the index back as a column.**

```python
# remove just year level and add as column
wdi_1 = df.set_index(["country", "year"])
wdi_1.reset_index(["year"])
```

      year
      GovExpend
      Consumption
      Exports
      Imports
      GDP

      country

      Canada
      2017
      0.372665
      1.095475
      0.582831
      0.600031
      1.868164

      Canada
      2016
      0.364899
      1.058426
      0.576394
      0.575775
      1.814016

      Canada
      2015
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

      Canada
      2014
      0.353485
      1.011988
      0.550323
      0.572344
      1.782252

      Canada
      2013
      0.351541
      0.986400
      0.518040
      0.558636
      1.732714

      ...
      ...
      ...
      ...
      ...
      ...
      ...

      United States
      2004
      2.267999
      9.311431
      1.335978
      2.108585
      13.846058

      United States
      2003
      2.233519
      8.974708
      1.218199
      1.892825
      13.339312

      United States
      2002
      2.193188
      8.698306
      1.192180
      1.804105
      12.968263

      United States
      2001
      2.112038
      8.480461
      1.213253
      1.740797
      12.746262

      United States
      2000
      2.040500
      8.272097
      1.287739
      1.790995
      12.620268

72 rows × 6 columns

2. **Completely throw away all levels of the index.**

```python
# throw away all levels of index
wdi_2 = df.set_index(["country", "year"])
wdi_2.reset_index()
```

      country
      year
      GovExpend
      Consumption
      Exports
      Imports
      GDP

      0
      Canada
      2017
      0.372665
      1.095475
      0.582831
      0.600031
      1.868164

      1
      Canada
      2016
      0.364899
      1.058426
      0.576394
      0.575775
      1.814016

      2
      Canada
      2015
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

      3
      Canada
      2014
      0.353485
      1.011988
      0.550323
      0.572344
      1.782252

      4
      Canada
      2013
      0.351541
      0.986400
      0.518040
      0.558636
      1.732714

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      67
      United States
      2004
      2.267999
      9.311431
      1.335978
      2.108585
      13.846058

      68
      United States
      2003
      2.233519
      8.974708
      1.218199
      1.892825
      13.339312

      69
      United States
      2002
      2.193188
      8.698306
      1.192180
      1.804105
      12.968263

      70
      United States
      2001
      2.112038
      8.480461
      1.213253
      1.740797
      12.746262

      71
      United States
      2000
      2.040500
      8.272097
      1.287739
      1.790995
      12.620268

72 rows × 7 columns

3. **Remove the `country` of the index and *do not* keep it as a column.**

```python
# Remove country from the index -- don't keep it as a column
wdi_3 = df.set_index(["country", "year"])
wdi_3.reset_index(["country"], drop=True)
```

      GovExpend
      Consumption
      Exports
      Imports
      GDP

      year

      2017
      0.372665
      1.095475
      0.582831
      0.600031
      1.868164

      2016
      0.364899
      1.058426
      0.576394
      0.575775
      1.814016

      2015
      0.358303
      1.035208
      0.568859
      0.575793
      1.794270

      2014
      0.353485
      1.011988
      0.550323
      0.572344
      1.782252

      2013
      0.351541
      0.986400
      0.518040
      0.558636
      1.732714

      ...
      ...
      ...
      ...
      ...
      ...

      2004
      2.267999
      9.311431
      1.335978
      2.108585
      13.846058

      2003
      2.233519
      8.974708
      1.218199
      1.892825
      13.339312

      2002
      2.193188
      8.698306
      1.192180
      1.804105
      12.968263

      2001
      2.112038
      8.480461
      1.213253
      1.740797
      12.746262

      2000
      2.040500
      8.272097
      1.287739
      1.790995
      12.620268

72 rows × 5 columns