Sepehr
Sepehr

Reputation: 452

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.

Upvotes: 5

Views: 2706

Answers (1)

YS-L
YS-L

Reputation: 14748

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

Upvotes: 6

Related Questions