<jupyter_start><jupyter_text>https://pandas.pydata.org/docs/user_guide/advanced.htmlMultiIndex<jupyter_code>arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
          ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]

tuples = list(zip(*arrays))
tuples
import pandas as pd
import numpy as np

index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])

iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
index2 = pd.MultiIndex.from_product(iterables, names=['first', 'second'])


df = pd.DataFrame([['bar', 'one'], ['bar', 'two'],
                    ['foo', 'one'], ['foo', 'two']],
                   columns=['first', 'second'])
index3 = pd.MultiIndex.from_frame(df)

index,index2,index3,index.names,index2.names,index3.names
s = pd.Series(np.random.randn(8), index=index)
s,pd.Series(np.random.randn(8), index=tuples)
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
          np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]

s = pd.Series(np.random.randn(8), index=arrays)
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
s,df,df.index.names
# 这里的index是没有names的
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
df1 = pd.DataFrame(np.random.randn(6, 6), index=index[:6], columns=index[:6])
df,df1

# 注意看跟上面的输出比较
with pd.option_context('display.multi_sparse', False):
    print(df)


index.get_level_values(0),index.get_level_values('second')

df['bar'],df['bar', 'one'],df['bar']['one'],s['qux']
df[['foo','qux']],\
df.columns.levels,\
df[['foo','qux']].columns.levels,df[['foo', 'qux']].columns.to_numpy(),\
df[['foo', 'qux']].columns.get_level_values(0),\
df[['foo', 'qux']].columns.remove_unused_levels().levels
s,s[:-2],s[::2],s + s[:-2],s + s[::2]
s.reindex(index[:3]),s.reindex([('foo', 'two'), ('bar', 'one'), ('qux', 'one'), ('baz', 'one')])
df,df.T,df.T.loc[('bar', 'two')],df.T.loc[('bar', 'two'), 'A']
df = df.T
df.loc['baz':'foo']

df.loc[('baz', 'two'):('qux', 'one')]
df.loc[('baz', 'two'):'foo']
df.loc[[('bar', 'two'), ('qux', 'one')]]
s = pd.Series([1, 2, 3, 4, 5, 6],
              index=pd.MultiIndex.from_product([["A", "B"], ["c", "d", "e"]]))

s,s.loc[[("A", "c"), ("B", "d")]] ,s.loc[(["A", "B"], ["c", "d"])]
# 使用切片
def mklbl(prefix, n):
     return ["%s%s" % (prefix, i) for i in range(n)]

miindex = pd.MultiIndex.from_product([mklbl('A', 4),
                                       mklbl('B', 2),
                                       mklbl('C', 4),
                                       mklbl('D', 2)])
miindex
micolumns = pd.MultiIndex.from_tuples([('a', 'foo'), ('a', 'bar'),
                                        ('b', 'foo'), ('b', 'bah')],
                                       names=['lvl0', 'lvl1'])
micolumns
dfmi = pd.DataFrame(np.arange(len(miindex) * len(micolumns))
                       .reshape((len(miindex), len(micolumns))),
                     index=miindex,
                     columns=micolumns).sort_index().sort_index(axis=1)

dfmi
dfmi.loc[(slice('A1', 'A3'), slice(None), ['C1', 'C3']), :]
idx = pd.IndexSlice
idx
dfmi.loc[idx[:, :, ['C1', 'C3']], idx[:, 'foo']]


dfmi.loc['A1', (slice(None), 'foo')]


dfmi.loc[idx[:, :, ['C1', 'C3']], idx[:, 'foo']]
mask = dfmi[('a', 'foo')] > 200
dfmi.loc[idx[mask, :, ['C1', 'C3']], idx[:, 'foo']]

dfmi.loc(axis=0)[:, :, ['C1', 'C3']]


df2 = dfmi.copy()
df2.loc(axis=0)[:, :, ['C1', 'C3']] = -10
df2
df2 = dfmi.copy()
df2.loc[idx[:, :, ['C1', 'C3']], :] = df2 * 1000
df2
df,df.xs('one', level='second'),df.loc[(slice(None), 'one'), :]


df = df.T
df.xs('one', level='second', axis=1),\
df.loc[:, (slice(None), 'one')],\
df.xs(('one', 'bar'), level=('second', 'first'), axis=1),\
df.loc[:, ('bar', 'one')],\
df.xs('one', level='second', axis=1, drop_level=False),\
df.xs('one', level='second', axis=1, drop_level=True)

midx = pd.MultiIndex(levels=[['zero', 'one'], ['x', 'y']],
                      codes=[[1, 1, 0, 0], [1, 0, 1, 0]])
df = pd.DataFrame(np.random.randn(4, 2), index=midx)
df
df2 = df.mean(level=0)
df2

df2.reindex(df.index, level=0)


df_aligned, df2_aligned = df.align(df2, level=0)

df_aligned, df2_aligned
df,df[:5],df[:5].swaplevel(0, 1, axis=0)
df[:5].reorder_levels([1, 0], axis=0)
df.rename(columns={0: "col0", 1: "col1"}),df.rename(index={"one": "two", "y": "z"})
df.rename_axis(index=['abc', 'def']),\
df.rename_axis(columns="Cols").columns

mi = pd.MultiIndex.from_product([[1, 2], ['a', 'b']], names=['x', 'y'])
mi2 = mi.rename("new name", level=0)
mi.names,mi2

import random
random.shuffle(tuples)
s = pd.Series(np.random.randn(8), index=pd.MultiIndex.from_tuples(tuples))

s,s.sort_index(),s.sort_index(level=0)
index = pd.Index(np.random.randint(0, 1000, 10))
positions = [0, 9, 3]
index,index[positions],index.take(positions)
ser = pd.Series(np.random.randn(10))
ser.iloc[positions],ser.take(positions)

frm = pd.DataFrame(np.random.randn(5, 3))
frm.take([1, 4, 3]),frm.take([0, 2], axis=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2776663993584603, 'fraction_numerical': 0.027465918203688853, 'mean_word_length': 4.132716049382716, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '221331', 'license': 'MIT', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2095, 'n_words': 365, 'path': 'pandas/user_guide/03_advanced.ipynb', 'repo_name': 'kingreatwill/penter', 'size': '4988'}