MulDataFrame.mloc#
- MulDataFrame.mloc#
Flexible hierachical indexing on the index and columns dataframes.
The row or the columns slicer can be a list or a dict. Check introduction to mloc ??? for detailed usage.
If a list is used, its length should be less than or equal to the columns length of the index or the columns dataframe. The hierarchical indexing order is from the leftmost column to the rightmost. Use
...as:in the list to select all elements in a column.If a dict is used, its keys should be the column names of the index or the columns dataframe and its values the slicers on the columns. The hierachical indexing order is the insertion order of the keys in the dict. Although Python does not guanrantee the insertion order, it is preserved in most cases. Use the OrderedDict class if you are really concerned about it.
Examples#
List indexing:
>>> 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']) >>> mf = MulDataFrame([[1,2],[8,9],[8,10]],index=index,columns=columns) >>> mf.mloc[[..., 6],[5]] (2,) g 7 f 5 c -------- ----- x y c b 3 6 b 8 b 5 6 b 8
Dictionary indexing:
>>> mf.mloc[:,{'g':6}] (3,) g 6 f 3 d ------- ------ x y d a 1 2 a 2 b 3 6 b 9 b 5 6 b 10 >>> mf.mloc[:,{'g':[6]}].shape (3,1) >>> mf.index.insert(2,'z',[1,2,3]) >>> mf.index.columns = ['x','y','y'] >>> mf.mloc[{'y':2}].name x 3 y 6 y 2 Name: b, dtype: int64
The last example shows that if the index or columns dataframe’s columns have duplicate names, use the last column for dict indexing. To select other columns of the same name, use list indexing or MulDataFrame.nloc.
Value assignment:
>>> md.mloc[[..., 2],{'g':6}] = 3 >>> md.iloc[0,1] 3
MulDataFrame