如何进行数据框的计算

How to proceed with calculations on dataframe

我不知道如何对该数据库进行计算。

数据库示例:

Indicator   Market      Sales     Costs    Volume
Real        Internal    30512     -16577   12469
Real        External    23        -15      8
Real        Other       65        -38      25
... ... ... ... ... ... ... ... ... ... ... ...
Budget      Internal    0.0        0.0     0.0
Budget      External    3.5       -2.3     60.0
Budget      Other       6.2       -3.9     90.8

首先,我需要通过添加销售额、成本和数量将“市场”合并为 1。即:

Indicator   Market      Sales     Costs    Volume
Real        Total       30600     -16630   12502
... ... ... ... ... ... ... ... ... ... ... ...
Budget      Total       9.7       -6.2     150.8

然后我需要用下面的公式计算“成本效应”:

成本效应:((实际costs/real量)-(预算cost/budget量)) x ((实际量+预算量)/2).

这将是: 成本效应:((-16630/12502)-(-6.2/150.8))*(12502+150.8)/2 = -8155.2

我试了一整天都没有结果。我应该为此使用 pandas 吗?

如有任何帮助,我们将不胜感激。

这个有效

# aggregate Costs and Volumes by Indicator
aggregate = df.groupby('Indicator')[['Costs', 'Volume']].sum()
#              Costs   Volume
# Indicator                  
# Budget        -6.2    150.8
# Real      -16630.0  12502.0
# plug the values into the cost effect formula
cost_effect = (aggregate.loc['Real', 'Costs'] / aggregate.loc['Real', 'Volume'] - aggregate.loc['Budget', 'Costs'] / aggregate.loc['Budget', 'Volume']) * aggregate['Volume'].sum() / 2
# -8155.19213384214
# the latter outcome can be derived a little more concisely by using the difference between the ratios
cost_effect = (aggregate['Costs'] / aggregate['Volume']).diff().iat[-1] * aggregate['Volume'].sum() / 2
# -8155.19213384214