Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert 2D dataframe to 3D dataframe

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?

like image 489
mohamad jumaa Avatar asked Mar 14 '26 08:03

mohamad jumaa


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
like image 121
jezrael Avatar answered Mar 17 '26 02:03

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!