Top "Multi-index" questions

A multi-index (also known as a hierarchical index) allows the manipulation of higher-dimensional data in a 2-dimensional tabular structure.

Converting a Pandas GroupBy output from Series to DataFrame

I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] , "City" : ["Seattle", "Seattle", "…

python pandas dataframe pandas-groupby multi-index
selecting from multi-index pandas

I have a multi-index data frame with columns 'A' and 'B'. Is there is a way to select rows by …

python pandas dataframe multi-index
Turn Pandas Multi-Index into column

I have a dataframe with 2 index levels: value Trial measurement 1 0 13 1 3 2 4 2 0 NaN 1 12 3 0 34 Which I want to turn into this: Trial measurement …

python pandas dataframe flatten multi-index
Construct pandas DataFrame from items in nested dictionary

Suppose I have a nested dictionary 'user_dict' with structure: Level 1: UserId (Long Integer) Level 2: Category (String) Level 3: Assorted Attributes (…

python pandas dataframe multi-index
How to query MultiIndex index columns values in pandas

Code example: In [171]: A = np.array([1.1, 1.1, 3.3, 3.3, 5.5, 6.6]) In [172]: B = np.array([111, 222, 222, 333, 333, 777]) In [173]: C = randint(10, 99, 6) In [174]: df = pd.DataFrame(zip(A, B, …

python pandas indexing slice multi-index
pandas dataframe select columns in multiindex

I have the following pd.DataFrame: Name 0 1 ... Col A B A B ... 0 0.409511 -0.537108 -0.355529 0.212134 ... 1 -0.332276 -1.087013 0.083684 0.529002 ... 2 1.138159 -0.327212 0.570834 2.337718 ... It has MultiIndex …

python pandas hierarchical multi-index
pandas: how to run a pivot with a multi-index?

I would like to run a pivot on a pandas DataFrame, with the index being two columns, not one. For …

python pandas pivot multi-index
Multi Index Sorting in Pandas

I have a dataset with multi-index columns in a pandas df that I would like to sort by values in …

python sorting pandas multi-index
How to remove levels from a multi-indexed dataframe?

For example, I have: In [1]: df = pd.DataFrame([8, 9], index=pd.MultiIndex.from_tuples([(1, 1, 1), (1, 3, 2)]), columns=['A']) In [2] df Out[2]: A 1 1 1 8 3 2 9 Is …

pandas dataframe multi-index
Pandas Plotting with Multi-Index

After performing a groupby.sum() on a DataFrame I'm having some trouble trying to create my intended plot. How can …

python matplotlib pandas multi-index