FenryrMKIII
FenryrMKIII

Reputation: 1198

Pandas interval multiindex

I need to create a data stucture allowing indexing via a tuple of floats. Each dimension of the tuple represents one parameter. Each parameter spans a continuous range and to be able to perform my work, I binned the range to categories.

Then, I want to create a dataframe with a MultiIndex, each dimension of the index referring to a parameter with the defined categories

import pandas as pd
import numpy as np
index = pd.interval_range(start=0, end=10, periods = 5, closed='both')
index2 = pd.interval_range(start=20, end=30, periods = 3, closed='both')
index3 = pd.MultiIndex.from_product([index,index2])
dataStructure = pd.DataFrame(np.zeros((5*3,1)), index = index3)
print(Qhat)

I checked that the interval_range provides me with the necessary methods e.g.

index.get_loc(2.5) 

would provide me the right answer. However I can't extend this with the dataframe nor the multiIndex

index3.get_loc((2.5,21))

does not work. Any idea ? I managed to get that working yesterday somehow therefore I am 99% convinced there is a simple way to make this work. But my jupyter notebook was in the cloud and the server crashed and notebook has been lost. I became dumber overnight apparently.

Upvotes: 0

Views: 279

Answers (1)

jezrael
jezrael

Reputation: 862441

I think select by tuple is not implemented yet, possible solution is get position for each level separately with Index.get_level_values, get intersection by intersect1d and last select by iloc:

idx1 = df.index.get_level_values(0).get_loc(2.5)
idx2 = df.index.get_level_values(1).get_loc(21)

df1 = df.iloc[np.intersect1d(idx1, idx2)]
print (df1)
                                     0
[2, 4] [20.0, 23.333333333333332]  0.0

Upvotes: 1

Related Questions