包含历史数据和未来数据的Python CSV需要分组并计算值

2024-10-02 12:23:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个5列csv文件,其中包括过去数据和未来数据(7月数据)。我需要使用python根据项对每一行进行分组。你可以在我的数据中看到两项,6200000和620001。我想根据每个项目过去数据的平均值/stdev计算未来数据ATP

Sample Data

下面是我当前的代码,但它只能应用于1项而不是多项,因为计算结果是错误的,我想知道我是否需要先创建一个字典。我是python新手,任何帮助都将不胜感激

total_data = pd.read_csv('file.csv')
present_data = total_data.dropna()
future_data=total_data[total_data['Actual Quantity'].isna()]  #I divided data by historical data and future data

present_data['Z'] = present_data['Actual Quantity'] / present_data['Scheduled Quantity']
present_data['logZ'] = np.log(present_data['Z'])
present_data['logX'] = np.log(present_data['Grade Yields']) #add 3 columns for calculation


sched = future_data.groupby('Item').Sched_Quantity.agg([('Sched_Quantity', ','.join)]).reset_index()

#Calculations

ProjectedProduction_list = []
atp_list = []
holdback_list =[]
PercentAtp_list = []

for i in present_data["Item.[Item]"].unique():

# print('For Item ID: '+ str(i))
datatemp = present_data[present_data['Item.[Item]'] == i]

string_item = str(i)


x = sched['Sched_Quantity'][sched.Item ==string_item ].tolist()

#Required y values  

y_list = x[0].split(',')
# logger.debug('y_list : {}'.format(y_list))
y_list = [int(float(i)) for i in y_list]

 

for y in y_list:      #loop for calculation
   
    mean1 = datatemp["Grade Yields"].mean()
    mean2 = datatemp["logZ"].mean()
    var1 = datatemp["logX"].var()
    var2 = datatemp["logZ"].var()
    ProjectedProduction = datatemp["Grade Yields"].mean() * y 
    ProjectedProduction_list.append(ProjectedProduction)
    CI = confidencelevel['Confidence Level']
    OutputAtp = mean1 * y * math.exp(mean2 - (norm.ppf(CI) * (math.sqrt(var1 + var2))))
    atp_list.append(OutputAtp)
    Holdback = ProjectedProduction - OutputAtp
    holdback_list.append(Holdback)
    PercentAtp = (OutputAtp/ProjectedProduction) * 100
    PercentAtp_list.append(PercentAtp)

 
future_data['ProjectedProduction'] = ProjectedProduction_list
future_data['OutputAtp'] = atp_list
future_data['Holdback'] = holdback_list
future_data['PercentAtp'] = PercentAtp_list

当前结果:项目混合在一起,因此ATP的结果不正确。我希望计算是基于每个项目,但我目前的代码混合了两个项目在一起。 Outcome of Future Data


Tags: 数据项目fordatafutureitemquantitylist

热门问题