MulDataFrame.set_index#

MulDataFrame.set_index(keys=None, mloc=None, drop=True, inplace=False)#

Add columns of MulDataFrame to its index dataframe.

The columns to be added to the index dataframe can be specified by the primary columns or by mloc indexing. When a muldataframe’s column (a MulSeries object) is added to the index dataframe, only its primary name is kept. Its name series is lost.

Parameters#

keyslabel or array-like or list of labels/arrays

Labels in the primary columns. It behave similarly to the keys parameter in DataFrame.set_index. It cannot be None if mloc is None.

mlocarray or dict

Hierachical indexing used to select columns. check mloc for possible values. This parameter is ignored if keys is not None. It cannot be None if keys is None.

dropbool, default True

Whether to delete columns to be added to the index dataframe.

inplacebool, default False

Whether to modify the MulDataFrame inplace rather than creating a new one.

Returns#

MulDataFrame or None

New MulDataFrame or None if inplace=True.

Examples#

>>> import pandas as pd
>>> import muldataframe as md
>>> index = pd.DataFrame([[1,2],[3,6],[5,6]],
             index=['a','b','b'],
             columns=['x','y'])
>>> columns = pd.DataFrame([[5,7],[3,6]],
                index=['c','d'],
                columns=['f','g'])
>>> md = MulDataFrame([[1,2],[8,9],[9,10]],index=index,columns=columns)
>>> md.set_index('c')
(3, 1)      g   6
            f   3
                d
----------  ------
   x  y  c      d
a  1  2  1  a   2
b  3  6  8  b   9
b  5  6  8  b  10
>>> md2 = md.set_index(mloc={'g':6})
(3, 1)       g  7
             f  5
                c
-----------  ------
   x  y   d     c
a  1  2   2  a  1
b  3  6   9  b  8
b  5  6  10  b  8