Linked Questions

27 votes
2 answers
18k views

MultiIndex-based indexing in pandas [duplicate]

If I define a hierarchically-indexed dataframe like this: import itertools import pandas as pd import numpy as np a = ('A', 'B') i = (0, 1, 2) b = (True, False) idx = pd.MultiIndex.from_tuples(list(...
kjo's user avatar
  • 35.7k
6 votes
2 answers
9k views

Slicing a MultiIndex DataFrame with a condition based on the index [duplicate]

I have a dataframe which looks like this: import pandas as pd import numpy as np arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']), np.array(['one', 'two', 'one', 'two', '...
crs's user avatar
  • 501
1 vote
1 answer
8k views

Filter Dataframe with MultiIndex by specific index value [duplicate]

I'm working in forecasting the demand of a product using many scenarios per year. I have a MulitiIndexed dataframe (Simulation, Year, Month) and need to filter by one of them (let's say Simulation). ...
Javier Lorenzini's user avatar
3 votes
1 answer
3k views

Slice pandas multiindex dataframe using list of index values [duplicate]

I have a multi-index dataframe that looks like uid tid text abc x t1 bcd y t2 uid and tid are the indexes. I have a list of uids, and want to get the rows corresponding to the uids in that ...
Rakib's user avatar
  • 7,613
3 votes
1 answer
941 views

python pandas multi-index select all within index level where criteria is met [duplicate]

This is a simplifeid version of my database. A B Store Product Year A 1 2014 12 63 2 2015 32 5 3 2016 45 0 B 1 2014 ...
jyh5's user avatar
  • 87
0 votes
1 answer
988 views

Selecting multiple columns from one level of a multi-indexed pandas dataframe [duplicate]

I am wondering if it is possible to select multiple items from one multi-index level? Say I have a pandas dataframe such as this: lvl_1 A B lvl_2 d c f e 0 -1....
tomthepeach's user avatar
0 votes
0 answers
308 views

"expected tuple but get str" when reindexing a dataframe [duplicate]

Here I have a dataframe and an array, please be aware that this dataframe has only 1 column, "ds" and "code" are indices. My purpose is to get rid of all the stocks in this ...
Cooper's user avatar
  • 83
1 vote
1 answer
241 views

How to filter pandas table based on multiple values from differen columns? [duplicate]

I have a pandas table in the following format [df], indexed by 'noc' and 'year'. How can I access a 'noc, year combination' and save the entry of 'total_medals' to a list? medal Bronze ...
Dominik Scheld's user avatar
0 votes
2 answers
48 views

Locate rows by particular label, found only in last multi-index level [duplicate]

After performing group-by, my new df has 3 level multindex. I need to access all rows with 'ZEBRA' labels; which is contained in the 3rd level index. I'm trying to use df.loc but unable to do so. I ...
pyeR_biz's user avatar
  • 1,054
150 votes
7 answers
246k views

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 ...
silencer's user avatar
  • 2,445
116 votes
8 answers
145k views

Filtering multiple items in a multi-index Pandas dataframe

I have the following table: Area NSRCODE PBL_AWI CM BONS 44705.492941 BTNN 253854.591990 FONG 41625....
Tristan Forward's user avatar
105 votes
7 answers
139k views

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 ...
AndyMoore's user avatar
  • 1,444
73 votes
13 answers
68k views

Selecting columns from pandas MultiIndex

I have DataFrame with MultiIndex columns that looks like this: # sample data col = pd.MultiIndex.from_arrays([['one', 'one', 'one', 'two', 'two', 'two'], ['a', 'b', 'c'...
metakermit's user avatar
  • 22.5k
104 votes
4 answers
68k views

What causes "indexing past lexsort depth" warning in Pandas?

I'm indexing a large multi-index Pandas df using df.loc[(key1, key2)]. Sometimes I get a series back (as expected), but other times I get a dataframe. I'm trying to isolate the cases which cause the ...
Josh Friedlander's user avatar
84 votes
3 answers
139k views

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)...
Vyacheslav Shkolyar's user avatar

15 30 50 per page