Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...