【问题标题】:Sum two variables by two specific columns and compute quotient按两个特定列对两个变量求和并计算商
【发布时间】:2020-11-23 18:09:00
【问题描述】:

我有一个数据框df1

    Plant name      Brand       Region  Units produced  capacity  Cost incurred
    Gujarat Plant   Hyundai     Asia    8500            9250      18500000
    Haryana Plant   Honda       Asia    10000           10750     21500000
    Chennai Plant   Hyundai     Asia    12000           12750     25500000
    Zurich Plant    Volkswagen  Europe  25000           25750     77250000
    Chennai Plant   Suzuki      Asia    6000            6750      13500000
    Rengensburg     BMW         Europe  12500           13250     92750000
    Dingolfing      Mercedes    Europe  14000           14750     103250000

我想要一个格式如下的输出数据框:

df2=    Region      BMW   Mercedes  Volkswagen  Toyota  Suzuki  Honda   Hyundai                             
        Europe
        North America
        Asia
        Oceania

对于特定的RegionBrand,每个单元格的内容等于sum(cost incurred) / sum(units produced)

我尝试过的代码,导致 ValueError:

for i,j in itertools.zip_longest(range(len(df2),range(len(df2.columns)):
    if (df2.index[i] in list(df1["Region"]) & df2.columns[j] in list(df1["Brand"])==True:
        temp1 = df1["Region"]==df2.index[i]
        temp2 = df1["Brand"]==df2.columns[j]] 
        df2.loc[df2.index[i],df2.columns[j]] = df1(temp1&temp2)["Cost incurred"].sum()/
                                            df1(temp1&temp2)["Units Produced"].sum()
    elif (df2.index[i] in list(df1["Region"]) & df2.columns[j] in list(df1["Brand"])==False:
        df2.loc[df2.index[i],df2.columns[j]] = 0

ValueError:具有多个元素的数组的真值是 模糊的。使用 a.any() 或 a.all()

【问题讨论】:

    标签: python excel pandas dataframe


    【解决方案1】:

    df.pivot_table() 专为枢轴和聚合功能而设计。一个快速(?)和肮脏的解决方案:

    df1.pivot_table(index="Region", columns="Brand", values="Cost incurred", aggfunc=np.sum)\
    / df1.pivot_table(index="Region", columns="Brand", values="Units produced", aggfunc=np.sum)
    

    输出

    Brand      BMW   Honda      Hyundai  Mercedes  Suzuki  Volkswagen
    Region                                                           
    Asia       NaN  2150.0  2146.341463       NaN  2250.0         NaN
    Europe  7420.0     NaN          NaN    7375.0     NaN      3090.0
    

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      相关资源
      最近更新 更多