简体   繁体   中英

How to create a hierarchical structure with dataframe

I am looking for a way to make it dynamic because the range of userlevels is wide.

Please help.

enter image description here

Since the intent of the question is not clear, if you want to create a hierarchical index, you can achieve this in the following way

import pandas as pd

idx = pd.MultiIndex.from_product(([21,22],[1,2,3,4,5],[60,70,80,90,100]), names=["uselevel","class","upper"])
pd.DataFrame(index=idx, columns=['column1','column2','column3'])

df.head(10)
            column1 column2 column3
uselevel    class   upper           
21  1   60  NaN NaN NaN
        70  NaN NaN NaN
        80  NaN NaN NaN
        90  NaN NaN NaN
       100  NaN NaN NaN
    2   60  NaN NaN NaN
        70  NaN NaN NaN
        80  NaN NaN NaN
        90  NaN NaN NaN
       100  NaN NaN NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM