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
41
votes
4 answers

Pandas reset index on series to remove multiindex

I have a Series that looks like this: 1999-03-31 SOLD_PRICE NaN 1999-06-30 SOLD_PRICE NaN 1999-09-30 SOLD_PRICE NaN 1999-12-31 SOLD_PRICE 3.00 2000-03-31 SOLD_PRICE 3.00 with an index that looks like: MultiIndex [(1999-03-31…
dartdog
  • 10,432
  • 21
  • 72
  • 121
37
votes
2 answers

Filling in date gaps in MultiIndex Pandas Dataframe

I would like to modify a pandas MultiIndex DataFrame such that each index group includes Dates between a specified range. I would like each group to fill in missing dates 2013-06-11 to 2013-12-31 with the value 0 (or NaN). Group A, Group B, Date, …
Michael
  • 385
  • 1
  • 3
  • 7
35
votes
4 answers

Selecting rows from a Pandas dataframe with a compound (hierarchical) index

I'm suspicious that this is trivial, but I yet to discover the incantation that will let me select rows from a Pandas dataframe based on the values of a hierarchical key. So, for example, imagine we have the following dataframe: import pandas df =…
JD Long
  • 59,675
  • 58
  • 202
  • 294
34
votes
5 answers

pandas: convert index type in multiindex dataframe

Hi have a multiindex dataframe: tuples = [('YTA_Q3', 1), ('YTA_Q3', 2), ('YTA_Q3', 3), ('YTA_Q3', 4), ('YTA_Q3', 99), ('YTA_Q3', 96)] # Index index = pd.MultiIndex.from_tuples(tuples, names=['Questions', 'Values']) # Columns columns =…
Boosted_d16
  • 13,340
  • 35
  • 98
  • 158
33
votes
2 answers

What are levels in a pandas DataFrame?

I've been reading through the documentation and many explanations and examples use levels as something taken for granted. Imho the docs lack a bit on a fundamental explanation of the data structure and definitions. What are levels in a data frame?…
Raf
  • 1,628
  • 3
  • 21
  • 40
33
votes
3 answers

Boxplot with pandas groupby multiindex, for specified sublevels from multiindex

Ok so I have a dataframe which contains timeseries data that has a multiline index for each columns. Here is a sample of what the data looks like and it is in csv format. Loading the data is not an issue here. What I want to do is to be able to…
pbreach
  • 16,049
  • 27
  • 82
  • 120
32
votes
1 answer

How to reindex a MultiIndex dataframe

Is there a way to reindex two dataframes (of differing levels) so that they share a common index across all levels? Demo: Create a basic Dataframe named 'A': index = np.array(['AUD','BRL','CAD','EUR','INR']) data = np.random.randint(1, 20, (5,5)) A…
BradB
  • 473
  • 2
  • 5
  • 6
31
votes
2 answers

Read multi-index on the columns from csv file

I have a .csv file that looks like this: Male, Male, Male, Female, Female R, R, L, R, R .86, .67, .88, .78, .81 I want to read that into a df, so that I have: Male Female R L R 0 .86 .67 .88 .78 .81 I did: df =…
nutship
  • 4,624
  • 13
  • 47
  • 64
30
votes
5 answers

Create multiindex from existing dataframe

I've spent hours browsing everywhere now to try to create a multiindex from dataframe in pandas. This is the dataframe I have (posting excel sheet mockup. I do have this in pandas dataframe): And this is what I want: I have tried newmulti =…
puifais
  • 738
  • 2
  • 9
  • 20
29
votes
2 answers

How to make two rows in a pandas dataframe into column headers

I have seen how to work with a double index, but I have not seen how to work with a two-row column headers. Is this possible? For example, row 1 is a repetitive series of dates: 2016, 2016, 2015, 2015 Row 2 is a repetitive series of data. Dollar…
Stephen
  • 437
  • 1
  • 5
  • 11
29
votes
2 answers

Giving a column multiple indexes/headers

I am working with pandas dataframes that are essentially time series like this: level Date 1976-01-01 409.67 1976-02-01 409.58 1976-03-01 409.66 … What I want to have, is multiple indexes/headers for the level column,…
JC_CL
  • 2,346
  • 6
  • 23
  • 36
27
votes
1 answer

Convert pandas group by object to multi-indexed Dataframe

If I have the following Dataframe >>> df = pd.DataFrame({'Name': ['Bob'] * 3 + ['Alice'] * 3, \ 'Destination': ['Athens', 'Rome'] * 3, 'Length': np.random.randint(1, 6, 6)}) >>> df Destination Length Name 0 Athens 3 Bob 1 …
beardc
  • 20,283
  • 17
  • 76
  • 94
25
votes
1 answer

Python Pandas: How to set the name of multiindex?

I want to add the name of index of multi-index dataframe. I want to set the name of red box in image as 'Ticker' How can I do that?
user3595632
  • 5,380
  • 10
  • 55
  • 111
24
votes
2 answers

pandas multiindex assignment from another dataframe

I am trying to understand pandas MultiIndex DataFrames and how to assign data to them. Specifically I'm interested in assigning entire blocks that match another smaller data frame. ix = pd.MultiIndex.from_product([['A', 'B'], ['a', 'b', 'c',…
Matti Lyra
  • 12,828
  • 8
  • 49
  • 67
24
votes
5 answers

Multi-Index Sorting in Pandas

I have a multi-index DataFrame created via a groupby operation. I'm trying to do a compound sort using several levels of the index, but I can't seem to find a sort function that does what I need. Initial dataset looks something like this (daily…
Keeth
  • 2,100
  • 2
  • 21
  • 29