简体   繁体   English

如何将循环结果存储在 dataframe 中?

[英]How can i store results from a loop in a dataframe?

I have the following loop creating kpi values for each of my 5 subsets.我有以下循环为我的 5 个子集创建 kpi 值。

list = [sku_1,sku_2,sku_3,sku_4,sku_5]
for i in list:
    df = triple_exp_smooth_mul(i, slen=12, extra_periods=4, alpha=0.3,beta=0.2, phi=0.9, gamma=0.2)
    kpi(df)
    print(kpi)

Output: 
Bias: 3.19, 1.06%
MAPE: 14.93%
MAE: 43.94, 14.64%
RMSE: 56.84, 18.94%
<function kpi at 0x000002634941C8B0>
Bias: -2.57, -1.31%
MAPE: 15.27%
MAE: 28.04, 14.27%
RMSE: 40.29, 20.50%
<function kpi at 0x000002634941C8B0>
Bias: -7.93, -3.77%
MAPE: 16.98%
MAE: 33.76, 16.08%
RMSE: 48.83, 23.25%
<function kpi at 0x000002634941C8B0>
Bias: -15.05, -2.98%
MAPE: 37.16%
MAE: 170.31, 33.75%
RMSE: 224.58, 44.50%
<function kpi at 0x000002634941C8B0>
Bias: 5.37, 2.03%
MAPE: 21.45%
MAE: 53.89, 20.41%
RMSE: 78.66, 29.79%
<function kpi at 0x000002634941C8B0>

Is there a way to store this in a nice table, something like this:有没有办法将它存储在一个漂亮的表中,如下所示:

KPI关键绩效指标 SKU1 Value SKU1 价值 SKU1 Scaled SKU1 缩放
BIAS偏见 xx xx % %
MAPE马佩 xx xx % %
MAE MAE xx xx % %
RMSE均方根误差 xx xx % %

With the SKU Value and SKU scaled in the columns?在列中缩放 SKU 值和 SKU?

Edit: Code used to create KPI function:编辑:用于创建 KPI function 的代码:

#Defining the function for measuring the forecast accuracy for each Forecast "#Credits to Nicolas Candeput "Data Science For Supply Chain Forecasting""
def kpi(df):
    dem_ave = df.loc[df['Error'].notnull(),'Demand'].mean()
    bias_abs = df['Error'].mean()
    bias_rel = bias_abs / dem_ave
    print('Bias: {:0.2f}, {:.2%}'.format(bias_abs,bias_rel))
    MAPE = (df['Error'].abs()/df['Demand']).mean()
    print('MAPE: {:.2%}'.format(MAPE))
    MAE_abs = df['Error'].abs().mean()
    MAE_rel = MAE_abs / dem_ave
    print('MAE: {:0.2f}, {:.2%}'.format(MAE_abs,MAE_rel))
    RMSE_abs = np.sqrt((df['Error']**2).mean())
    RMSE_rel = RMSE_abs / dem_ave
    print('RMSE: {:0.2f}, {:.2%}'.format(RMSE_abs,RMSE_rel))

The first thing to understand is that printing something inside a function isn't the same as returning it.首先要了解的是,在 function 中打印某些内容与返回它不同。 See How is returning the output of a function different from printing it?请参阅返回 function 的 output 与打印有何不同?

I also second Tim's suggestion that Bias, etc. should be columns , and you should have rows for each SKU.我也支持 Tim 的建议,即 Bias 等应该是columns ,并且每个 SKU 都应该有行。

You should return the values you want from your function, and then use those returned values to add a row to your dataframe.您应该从 function返回所需的值,然后使用这些返回的值将一行添加到 dataframe。 We will return the values as a dictionary, save all returned dicts to a list, and once we're done with all SKUs, create a dataframe from this list of dicts using the constructor (see Convert list of dictionaries to a pandas DataFrame ).我们将把值返回为字典,将所有返回的命令保存到列表中,一旦我们完成了所有SKU,就可以使用构造函数列表中的dicts列表中创建Z6A8064B5DF4794555555557DZ

So first, let's modify our kpi() function to return a dict whose keys are the column names, and values are the values we want for that row.所以首先,让我们修改我们的kpi() function 以返回一个字典,其键是列名,值是我们想要该行的值。

def kpi(df):
    ret = {}
    dem_ave = df.loc[df['Error'].notnull(),'Demand'].mean()
    bias_abs = df['Error'].mean()
    bias_rel = bias_abs / dem_ave
    ret['Bias'] = bias_abs
    ret['Bias %'] = bias_rel
    # print('Bias: {:0.2f}, {:.2%}'.format(bias_abs,bias_rel))
    MAPE = (df['Error'].abs()/df['Demand']).mean()
    ret['MAPE'] = MAPE
    # print('MAPE: {:.2%}'.format(MAPE))
    MAE_abs = df['Error'].abs().mean()
    MAE_rel = MAE_abs / dem_ave
    ret['MAE'] = MAE_abs
    ret['MAE %'] = MAE_rel
    # print('MAE: {:0.2f}, {:.2%}'.format(MAE_abs,MAE_rel))
    RMSE_abs = np.sqrt((df['Error']**2).mean())
    RMSE_rel = RMSE_abs / dem_ave
    ret['RMSE'] = RMSE_abs
    ret['RMSE %'] = RMSE_rel
    # print('RMSE: {:0.2f}, {:.2%}'.format(RMSE_abs,RMSE_rel))
    return ret

Now, let's create an empty list to hold our returned dicts and fill it up:现在,让我们创建一个空列表来保存我们返回的字典并填充它:

sku_list = [sku_1,sku_2,sku_3,sku_4,sku_5]
result_list = []
for sku in sku_list:
    df = triple_exp_smooth_mul(sku, slen=12, extra_periods=4, alpha=0.3,beta=0.2, phi=0.9, gamma=0.2)
    result_list.append(kpi(df))

You could write this as a list comprehension instead of a loop like so:你可以把它写成一个列表推导而不是像这样的循环:

result_list = [kpi(
                   triple_exp_smooth_mul(sku, slen=12, extra_periods=4, alpha=0.3,beta=0.2, phi=0.9, gamma=0.2)
                   ) 
               for sku in sku_list]

Then, create a dataframe out of it:然后,从中创建一个 dataframe :

result_df = pd.DataFrame(result_list)

Note that I renamed your list to sku_list because it's not a good idea to shadow builtins .请注意,我将您的list重命名为sku_list因为隐藏 builtins 不是一个好主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM