Questions tagged [multi-index]

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

A multi-index exists when an indexing column (or row) of a tabular structure contains more than one line. The use of layered indexing enables one to effectively store and manipulate arbitrarily high dimension data in a 2-dimensional tabular structure 1. This is also called "hierarchical" or "multi-level" indexing.

2771 questions
673
votes
12 answers

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", "Portland", "Seattle", "Seattle", "Portland"] } ) Which when printed…
saveenr
  • 8,439
  • 3
  • 19
  • 20
541
votes
19 answers

How to flatten a hierarchical index in columns

I have a data frame with a hierarchical index in axis 1 (columns) (from a groupby.agg operation): USAF WBAN year month day s_PC s_CL s_CD s_CNT tempf sum sum sum sum amax amin 0 …
Ross R
  • 8,853
  • 7
  • 28
  • 27
314
votes
7 answers

Select rows in pandas MultiIndex DataFrame

What are the most common pandas ways to select/filter rows of a dataframe whose index is a MultiIndex? Slicing based on a single value/label Slicing based on multiple labels from one or more levels Filtering on boolean conditions and…
cs95
  • 379,657
  • 97
  • 704
  • 746
258
votes
8 answers

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 …
TheChymera
  • 17,004
  • 14
  • 56
  • 86
154
votes
7 answers

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 (floats, ints, etc..) For example, an entry of this dictionary would be: user_dict[12] = { …
vladimir montealegre
  • 2,010
  • 2
  • 15
  • 17
142
votes
7 answers

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 filtering on one column of the multi-index without resetting the index to a single column index? For Example. # has multi-index (A,B) df #can I do this?…
silencer
  • 2,285
  • 5
  • 18
  • 18
121
votes
3 answers

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 …
wfh
  • 1,553
  • 3
  • 12
  • 10
108
votes
2 answers

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 a specific column. My dataset looks like: Group1 Group2 A B C A B C 1 1 0 3 2 5 7 2 5 6 9 1 0 0 3 7 0 2 0 3 5 I want…
MattB
  • 1,325
  • 2
  • 11
  • 8
104
votes
4 answers

Pandas: add a column to a multiindex column dataframe

I would like to add a column to the second level of a multiindex column dataframe. In [151]: df Out[151]: first bar baz second one two one two A 0.487880 -0.487661 -1.030176 0.100813…
user1642513
100
votes
6 answers

Nested dictionary to multiindex dataframe where dictionary keys are column labels

Say I have a dictionary that looks like this: dictionary = {'A' : {'a': [1,2,3,4,5], 'b': [6,7,8,9,1]}, 'B' : {'a': [2,3,4,5,6], 'b': [7,8,9,1,2]}} and I want a dataframe that looks something…
pbreach
  • 16,049
  • 27
  • 82
  • 120
93
votes
7 answers

How to select second level in multiindex when using columns?

I have a dataframe with this index: index = pd.MultiIndex.from_product([['stock1','stock2'...],['price','volume'...]]) It's a useful structure for being able to do df['stock1'], but how do I select all the price data? I can't make any sense of the…
AndyMoore
  • 1,324
  • 2
  • 11
  • 18
89
votes
3 answers

Concatenate Pandas columns under new multi-index level

Given a dictionary of data frames like: dict = {'ABC': df1, 'XYZ' : df2} # of any length... where each data frame has the same columns and similar index, for example: data Open High Low Close Volume Date …
Zero
  • 11,593
  • 9
  • 52
  • 70
88
votes
5 answers

Pandas dataframe with multiindex column - merge levels

I have a dataframe, grouped, with multiindex columns as below: import pandas as pd import numpy as np import random codes = ["one","two","three"]; colours = ["black", "white"]; textures = ["soft", "hard"]; N= 100 # length of the dataframe df =…
Zhubarb
  • 11,432
  • 18
  • 75
  • 114
85
votes
8 answers

Resampling Within a Pandas MultiIndex

I have some hierarchical data which bottoms out into time series data which looks something like this: df = pandas.DataFrame( {'value_a': values_a, 'value_b': values_b}, index=[states, cities, dates]) df.index.names = ['State', 'City',…
83
votes
3 answers

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, C), columns=['A', 'B', 'C']) In [175]:…
Vyacheslav Shkolyar
  • 1,926
  • 2
  • 14
  • 12
1
2 3
99 100