1

I have four dfs

dfB = pd.DataFrame([[cheapest_brandB[0],wertBereichB]], columns=['brand', 'price'], index= 
       ['cheap'])
dfC = pd.DataFrame([[cheapest_brandC[0],wertBereichC]], columns=['brand', 'price'], index= 
      ['cheap'])
dfG = pd.DataFrame([[cheapest_brandG[0],wertBereichG]], columns=['brand', 'price'], index= 
      ['cheap'])
dfO = pd.DataFrame([[cheapest_brandO[0],wertBereichO]], columns=['brand', 'price'], index= 
      ['cheap'])

the result :


        brand                             price
cheap  ASUS                {'gte': 821.84, 'lte': 1200.91}

        brand                            price
cheap    HP                {'gte': 187.82, 'lte': 993.73}

        brand                            price
cheap  Google              {'gte': 1047.3, 'lte': 2093.59}

        brand                            price
cheap   MSI                {'gte': 1047.3, 'lte': 2093.59}

and I want to make 3d df so that each of them belongs to a specific index something like that

                 Gaming                                         Casual                      ....

        brand             price                     brand           price
cheap   ASUS    {'gte': 821.84, 'lte': 1200.91}     HP      {'gte': 187.82, 'lte': 993.73}    ....
light   ..                 ..                       ..                 ..

  

1 Answers1

1

With the dataframes you provided:

import pandas as pd

dfs = [
    pd.DataFrame(
        {"brand": ["ASUS"], "price": [{"gte": 821.84, "lte": 1200.91}]}, index=["cheap"]
    ),
    pd.DataFrame(
        {"brand": ["HP"], "price": [{"gte": 187.82, "lte": 993.73}]}, index=["cheap"]
    ),
    pd.DataFrame(
        {"brand": ["Google"], "price": [{"gte": 1047.3, "lte": 2093.59}]},
        index=["cheap"],
    ),
    pd.DataFrame(
        {"brand": ["MSI"], "price": [{"gte": 1047.3, "lte": 2093.59}]}, index=["cheap"]
    ),
]

Here is one way to it:

for df, name in zip(dfs, ["Gaming", "Casual", "Pro", "Other"]):
    df.columns = pd.MultiIndex.from_product([[name], ["brand", "price"]])

df = pd.concat(dfs, axis=1)

And so:

print(df)
# Output
      Gaming                                  Casual  \
       brand                            price  brand   
cheap   ASUS  {'gte': 821.84, 'lte': 1200.91}     HP   

                                          Pro  \
                                price   brand   
cheap  {'gte': 187.82, 'lte': 993.73}  Google   

                                       Other
                                 price brand                            price  
cheap  {'gte': 1047.3, 'lte': 2093.59}   MSI  {'gte': 1047.3, 'lte': 2093.59}  
Laurent
  • 12,287
  • 7
  • 21
  • 37
  • thank you very mach , Can you tell me how can I access the data? I mean for example how do I get to asus or hp for gaming – mohamad jumaa Jul 06 '22 at 19:56
  • `df.loc[:, ("Gaming", "brand")]` for instance. See https://pandas.pydata.org/docs/user_guide/advanced.html#advanced-indexing-with-hierarchical-index for additional guidance. Cheers. – Laurent Jul 07 '22 at 04:24
  • Sorry, does it work on your device, did you try it with you because I tried it and opened the link and read it, but I did not benefit from anything also it does not work – mohamad jumaa Jul 07 '22 at 10:19
  • Yes, the statement work on my part, so I'm not sure to understand your issue here, sorry. The link is about querying multiindex dataframe, which is what you were asking about, if I'me not mistaken. Cheers. – Laurent Jul 07 '22 at 21:00