简体   繁体   中英

Creating a dataframe with MultiIndex columns from a dictionary

Using the following dictionary:

dic = {'S1':["2013-11-12", "2013-11-13"],
       'S2':["2013-11-15", "2013-11-17"]}

How can I create the following DataFrame with multiple column indices?

             S1                             S2                      
    Start          Stop            Start          Stop     
 2013-11-12     2013-11-13      2013-11-15     2013-11-17

Any help is appreciated.

You could do this:

index = pd.MultiIndex.from_product([['S1', 'S2'], ['Start', 'Stop']])
print pd.DataFrame([pd.DataFrame(dic).unstack().values], columns=index)

Output:

           S1                      S2            
        Start        Stop       Start        Stop
0  2013-11-12  2013-11-13  2013-11-15  2013-11-17

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