Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a dataframe with MultiIndex columns from a dictionary

Tags:

python

pandas

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.

like image 606
Sepehr Avatar asked Sep 10 '15 14:09

Sepehr


1 Answers

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
like image 85
YS-L Avatar answered Oct 06 '22 00:10

YS-L