首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >make和3d数据

make和3d数据
EN

Stack Overflow用户
提问于 2022-06-27 12:55:42
回答 1查看 55关注 0票数 1

我有四个外勤部

代码语言:javascript
复制
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'])

结果:

代码语言:javascript
复制
        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}

我想要制作3d df,以便它们每个都属于一个特定的索引,就像这样。

代码语言:javascript
复制
                 Gaming                                         Casual                      ....

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-03 17:23:24

在您提供的数据文件中:

代码语言:javascript
复制
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"]
    ),
]

有一种方法可以做到:

代码语言:javascript
复制
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)

因此:

代码语言:javascript
复制
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}  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72772472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档