|
本帖最后由 a10322495 于 2021-8-20 15:34 编辑
NO3 :建立动态变量 , 且给动态变量赋值的例子 :
for name in ['fh','fy','jh','sj']:
s='code_ratio_%s'%(name)
exec(s+'=%s' %(4))
code_ratio_sj,code_ratio_fy,code_ratio_jh,code_ratio_sj
=======================================================================import numpy as np
import pandas as pd
list_name=[]
for name in ['fh','fy','jh','sj']:
target1='code_ratio_%s'%(name)
exec(target1+'=%f' %(df2_top1.query('type==@name').apply(lambda x :'%s' %(round(x['code_cnt_sj']/x['code_cnt_all'],4)),axis=1)))
target2='sku_cnt_ratio_'+name
exec(target2+'=%f' %(df2_top1.query('type==@name').apply(lambda x :'%s' %(round(x['sku_cnt_sj']/x['sku_cnt_all'],4)),axis=1)))
target3='sku_sum_ratio_'+name
exec(target3+'=%f' %(df2_top1.query('type==@name').apply(lambda x :'%s' %(round(x['sku_sum_sj']/x['sku_sum_all'],4)),axis=1)))
target4='pcs_sum_ratio_'+name
exec(target4+'=%f' %(df2_top1.query('type==@name').apply(lambda x :'%s' %(round(x['pcs_sum_sj']/x['pcs_sum_all'],4)),axis=1)))
target5='pcs_sum_with_ydl_ratio_'+name
exec(target5+'=%f' %(df2_top1.query('type==@name').apply(lambda x :'%s' %(round(x['pcs_sum_with_ydl_sj']/x['pcs_sum_with_ydl_all'],4)),axis=1)))
targetall='mean_'+name
exec(targetall+'=%f' %(df2_top1.query('type==@name').apply(lambda x :np.mean([x['pcs_sum_with_ydl_sj']/x['pcs_sum_with_ydl_all']
,x['code_cnt_sj']/x['code_cnt_all']
,x['sku_cnt_sj']/x['sku_cnt_all']
,x['pcs_sum_sj']/x['pcs_sum_all']
,x['pcs_sum_sj']/x['pcs_sum_all']]),axis=1)))
list_name.extend([target1,target2,target3,target4,target5,targetall])
# print(code_ratio_sj,sku_cnt_ratio_sj,sku_sum_ratio_sj,pcs_sum_ratio_sj,pcs_sum_with_ydl_ratio_sj,mean_sj)
for i in list_name:
get_var=locals()
print(i,get_var)
# get_var['sku_sum_ratio_fy']
|
|