问题

假设我们有以下数据帧:

 import pandas as pd

df_dict = {'term1':[0.87,0.65],'term2':[0.87,0.40],
               'term3':[0.87,0.55],'term4':[0.87,0.70],
              'term5':[0.87,0.85],'term6':[0.87,0.90],
              }

df = pd.DataFrame(df_dict,index = ['Revenue','Cost'])
 

这给了我们:

enter image description here

相继增加成本的最佳方法是什么,比如,在term4的利润为负数之前,所有terms的成本均匀增加1%?是否可以将结果保存在绘图准备中(x-轴=利润,y-轴=成本增加%)?

  最佳答案

有了一点数学,您正在寻找的数字是这个的term4:

 np.log(df.loc['Revenue']/df.loc['Cost'])/np.log(1.01)
 

这是:

 term1    29.297604
term2    78.090736
term3    46.086401
term4    21.849814
term5     2.337295
term6    -3.407078
dtype: float64
 

所以这里的答案是22.将所有内容放在一起:

 num = np.ceil(np.log(df.loc['Revenue']/df.loc['Cost'])
                  .div(np.log(1.01))).loc['term4']
num = int(num)

inc_cost = pd.DataFrame(df.loc['Cost'].values * np.cumprod([1.01]*num)[:,None],
             columns=df.columns,
             index=[f'Cost_{i}' for i in range(1,num+1)])
 

输出:

             term1     term2     term3     term4     term5     term6
Cost_1   0.656500  0.404000  0.555500  0.707000  0.858500  0.909000
Cost_2   0.663065  0.408040  0.561055  0.714070  0.867085  0.918090
Cost_3   0.669696  0.412120  0.566666  0.721211  0.875756  0.927271
Cost_4   0.676393  0.416242  0.572332  0.728423  0.884513  0.936544
Cost_5   0.683157  0.420404  0.578056  0.735707  0.893359  0.945909
Cost_6   0.689988  0.424608  0.583836  0.743064  0.902292  0.955368
Cost_7   0.696888  0.428854  0.589674  0.750495  0.911315  0.964922
Cost_8   0.703857  0.433143  0.595571  0.758000  0.920428  0.974571
Cost_9   0.710895  0.437474  0.601527  0.765580  0.929632  0.984317
Cost_10  0.718004  0.441849  0.607542  0.773235  0.938929  0.994160
Cost_11  0.725184  0.446267  0.613618  0.780968  0.948318  1.004102
Cost_12  0.732436  0.450730  0.619754  0.788778  0.957801  1.014143
Cost_13  0.739761  0.455237  0.625951  0.796665  0.967379  1.024284
Cost_14  0.747158  0.459790  0.632211  0.804632  0.977053  1.034527
Cost_15  0.754630  0.464388  0.638533  0.812678  0.986824  1.044872
Cost_16  0.762176  0.469031  0.644918  0.820805  0.996692  1.055321
Cost_17  0.769798  0.473722  0.651367  0.829013  1.006659  1.065874
Cost_18  0.777496  0.478459  0.657881  0.837303  1.016725  1.076533
Cost_19  0.785271  0.483244  0.664460  0.845676  1.026893  1.087298
Cost_20  0.793124  0.488076  0.671105  0.854133  1.037162  1.098171
Cost_21  0.801055  0.492957  0.677816  0.862674  1.047533  1.109153
Cost_22  0.809065  0.497886  0.684594  0.871301  1.058008  1.120244
 

  相同标签的其他问题

pythonpandas