Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing all index values when using multiIndexing in Pandas

I would like that when viewing my DataFrame I will see all values of the multiIndex, including when subsequent rows have the same index for one of the levels. Here is an example:

arrays = [['20', '50', '20', '20'],['N/A', 'N/A', '10', '30']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['Jim', 'Betty'])
pd.DataFrame([np.random.rand(1)]*4,index=index)

The output is:

                                0
Jim         Betty           
20          N/A          0.954973
50          N/A          0.954973
20          10           0.954973
            30           0.954973

I would like to have a 20 also in the south-west corner. That is, I would like my DataFrame to be:

                                0
Jim         Betty           
20          N/A          0.954973
50          N/A          0.954973
20          10           0.954973
20          30           0.954973

Is Pandas capable of doing that?

like image 451
splinter Avatar asked Jul 05 '16 19:07

splinter


People also ask

Does ILOC include index?

As previously mentioned, Pandas iloc is primarily integer position based. That is, it can be used to index a dataframe using 0 to length-1 whether it's the row or column indices.


1 Answers

You need set display.multi_sparse to False:

#if need temporary use option
with pd.option_context('display.multi_sparse', False):
    print (df)

                  0
Jim Betty          
20  N/A    0.201643
50  N/A    0.201643
20  10     0.201643
20  30     0.201643

If this display option is required throughout a notebook, the option can be set once and for all as follows:

# if permanent use
import pandas as pd
pd.options.display.multi_sparse = False

Documentation:

display.multi_sparse
True
“Sparsify” MultiIndex display (don’t display repeated elements in outer levels within groups)

like image 131
jezrael Avatar answered Sep 21 '22 01:09

jezrael