我正在做一个Django项目。为了方便起见,我将模型转换为数据框。我被困在获得“ Sales(L)”列的Keyerror的步骤中。如果我在shell或spyder中运行相同的脚本,则不会引发任何错误。在下面看看我的脚本。
smtv = MTD.pdobjects.all()
se = Salesexecutive.pdobjects.all()
smtv_df = smtv.to_dataframe().reset_index(drop=True)
se_df = se.to_dataframe().reset_index(drop=True)
se_df=se_df.rename(columns={'Customer_Account':'Customer_Code'})
smtv_df['Sales_Value']=smtv_df['Sales_Value'].astype(float)
smtv_df['Sales_Value']=(smtv_df['Sales_Value']/100000).apply(lambda x: round(x, 2))
smtv_df=smtv_df[(smtv_df['Invoice_Date']>=month_st) & (smtv_df['Invoice_Date']<=tdt)]
df = pandas.merge(smtv_df,se_df[['Customer_Code','Ecode','Sales_Executive']],on='Customer_Code', how='left')
df[['Ecode', 'Sales_Executive']] = df[['Ecode','Sales_Executive']].fillna('Unknown')
filt_df={}
filt_df['RGN']=request.POST.get('RGN', None)
filt_df['MKU']=request.POST.get('MKU', None)
filt_df['Vertical']=request.POST.get('Vertical', None)
filt_df['Channel']=request.POST.get('Channel', None)
fikt={k: v for k, v in filt_df.items() if v!=''}
for k,v in fikt.items():
df=df[df[k]==v]
df1=pandas.pivot_table(df,index='MKU',values=['Sales_Value'],aggfunc='sum').replace(numpy.nan,0)
df1=df1.rename(columns={'Sales_Value':'Sales(L)'})
df1['Sale Target(L)']=df1['Sales(L)']*1.25
请提出一些想法来解决此问题。 谢谢