convert 2D dataframe to 3D dataframe

Viewed 42

I have a T 2D dataframe as Follow :

dfB = pd.DataFrame([[cheapest_brandB[0],wertBereichB]], columns=['brand', 'price'], index= 
       ['cheap'])
dfC = pd.DataFrame([[cheapest_brandC[0],wertBereichC]], columns=['brand', 'price'], index= 
      ['cheap'])

the Result :
        brand                            price
cheap   ASUS                              200

        brand                            price
cheap    HP                                500

How can I merge the 2d and turn it into a 3d dataframe, where every 2d columns are under the name of Gaming or casual.

i want somthing like that :

                Gaming                                         Casual                     
        brand             price                       brand           price
cheap   ASUS               200                       HP               500

Can anyone help me?

1 Answers

Use concat with keys parameter:

df = pd.concat([dfB, dfC], axis=1, keys=('Gaming','Casual'))
print (df)
      Gaming       Casual      
       brand price  brand price
cheap   ASUS   200     HP   500
Related