<jupyter_start><jupyter_text>Tutorial 13-Hierarchical Indexing<jupyter_code>import numpy as np
import pandas as pd
data=pd.Series(np.random.randn(8),
               index=[["a","a","a","b",
                       "b","b","c","c"],
                      [1,2,3,1,2,3,1,2]])
data
data.index
data["a"]
data["b":"c"]
data.loc[["a","c"]]
data.loc[:,1]
data.unstack()
data.unstack().stack()
df=pd.DataFrame(
    np.arange(12).reshape(4,3),
    index=[["a","a","b","b"],
           [1,2,1,2]],
    columns=[["num","num","ver"],
             ["math","stat","geo"]])
df
df.index.names=["class","exam"]
df.columns.names=["field","lesson"]
df
df["num"]
df.swaplevel("class","exam")
df.sort_index(level=1)<jupyter_output><empty_output><jupyter_text>Summary Statistics by Level<jupyter_code>df.sum(level="exam")
df.sum(level="field",axis=1)<jupyter_output><empty_output><jupyter_text>Indexing with a DataFrame’s columns<jupyter_code>data=pd.DataFrame(
    {"x":range(8),"y":range(8,0,-1),
     "a":["one","one","one","one","two",
          "two","two","two"],
     "b":[0,1,2,3,0,1,2,3]})
data
data2=data.set_index(["a","b"])
data2
data3=data.set_index(["a","b"],drop=False)
data3
data2
data2.reset_index()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2526573998364677, 'fraction_numerical': 0.032706459525756335, 'mean_word_length': 5.12, 'pattern_counts': {'":': 5, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '95425', 'license': 'MIT', 'n_tokens_mistral': 514, 'n_tokens_neox': 504, 'n_words': 54, 'path': 'jupyter/pandas-tut/13-Hierarchical Indexing.ipynb', 'repo_name': 'Sesughter01/ds-march-univel', 'size': '1223'}