将2维数据框转换为3维数据框。

3

我有一个如下所示的 T 2D 数据框:

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

我如何将2D数据合并成3D数据框,其中每个2D列都位于“游戏”或“休闲”下方的名称之下?
i want somthing like that :

                Gaming                                         Casual                     
        brand             price                       brand           price
cheap   ASUS               200                       HP               500

有人能帮我吗?

1个回答

1
使用带有keys参数的concat函数:concat
df = pd.concat([dfB, dfC], axis=1, keys=('Gaming','Casual'))
print (df)
      Gaming       Casual      
       brand price  brand price
cheap   ASUS   200     HP   500

你能告诉我如何访问数据吗?我的意思是,例如我如何访问华硕或惠普的数据。 - mohamad jumaa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接