<jupyter_start><jupyter_text>Multi-Level DataFrame<jupyter_code>import numpy as np
import pandas as pd
from numpy.random import randn

outside = 'G1 G1 G1 G2 G2 G2'.split()
inside = [1, 2, 3, 1, 2, 3]
list(zip(outside, inside))
hier_index = pd.MultiIndex.from_tuples(list(zip(outside, inside)))
hier_index
df = pd.DataFrame(randn(6,2), hier_index, ['A', 'B'])
df<jupyter_output><empty_output><jupyter_text>Selecting a ValueHeres how you select a value:<jupyter_code>df.loc['G1', 1]['B']<jupyter_output><empty_output><jupyter_text>Naming the unnamed indexs:<jupyter_code>df.index.names = ['Groups', 'Numbers']
df<jupyter_output><empty_output><jupyter_text>Cross-sectionSelect by index level<jupyter_code>df.xs(key=1,level=1)<jupyter_output><empty_output><jupyter_text>or index name:<jupyter_code>df.xs(key=1,level='Numbers')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1488862837045721, 'fraction_numerical': 0.022274325908558032, 'mean_word_length': 10.236842105263158, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '42198', 'license': 'Apache-2.0', 'n_tokens_mistral': 360, 'n_tokens_neox': 344, 'n_words': 58, 'path': 'pandas/PandasMultiLevelDataFrame.ipynb', 'repo_name': 'data-science-tsb/python-libraries', 'size': '853'}