%matplotlib inline

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
try:
    import seaborn
except ImportError:
    pass

pd.options.display.max_rows = 10

"""
Explanation: Advanced indexing
End of explanation
"""


cast = pd.read_csv('data/cast.csv')
cast.head()

titles = pd.read_csv('data/titles.csv')
titles.head()

"""
Explanation: This dataset is borrowed from the PyCon tutorial of Brandon Rhodes (so all credit to him!). You can download these data from here: titles.csv and cast.csv and put them in the /data folder.
End of explanation
"""


c = cast.set_index('title')

c.head()

"""
Explanation: Setting columns as the index
Why is it useful to have an index?

Giving meaningful labels to your data -> easier to remember which data are where
Unleash some powerful methods, eg with a DatetimeIndex for time series
Easier and faster selection of data

It is this last one we are going to explore here!
Setting the title column as the index:
End of explanation
"""


%%time
cast[cast['title'] == 'Hamlet']

"""
Explanation: Instead of doing:
End of explanation
"""


%%time
c.loc['Hamlet']

"""
Explanation: we can now do:
End of explanation
"""


c = cast.set_index(['title', 'year'])

c.head()

%%time
c.loc[('Hamlet', 2000),:]

c2 = c.sort_index()

%%time
c2.loc[('Hamlet', 2000),:]

"""
Explanation: But you can also have multiple columns as the index, leading to a multi-index or hierarchical index:
End of explanation
"""


