for i in e.columns: if e[i].dtype==np.float64: e[i]*=16111
关于enumerate
1 2 3 4 5 6 7 8 9
deffunc(tb,path): newT=defaultdict(int) for i inrange(len(tb)): if (k:=mapTable[tb["Value"][i]]) in v["Unnamed: 0"].unique(): # unique()才能用 in,否则数据类型不是list _=tb['Count'][i]/100 for n,g inenumerate(v.columns[2:].tolist(),2): # enumerate(list,idx) idx表示起始下标 newT[g]+=_/1e10*v[v["Unnamed: 0"]==k].iloc[0,n] s=pd.DataFrame(newT,range(1)) s.to_csv(path)
很酷的运算1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
AT=[] tab=os.listdir(path) tables=[i for i in tab if i.endswith("t.csv")] # ['2017t.csv', '2018t.csv', '2019t.csv', '2020t.csv', '2021t.csv', '2022t.csv'] for i inrange(len(tables)): newT=defaultdict(int) year=tables[i].split('.')[0] tb=pd.read_csv(os.path.join(path,tables[i])) for i inrange(len(tb)): if (k:=mapTable[tb["Value"][i]]) in v["Unnamed: 0"].unique(): _=tb['Count'][i]/100 for n,g inenumerate(v.columns[2:].tolist(),2): # 每一项都等于 每种类型的像元乘以该像元在该项之下的价值之和 newT[g]+=_/1e10*v[v["Unnamed: 0"]==k].iloc[0,n] AT.append(newT) d=pd.DataFrame(AT,index=["%s"%i for i inrange(2017,2023)])
defgetValue(year): deff(tem): Sum=0 for i inrange(len(tem)): # 若映射存在 if(k:=mapTable[tem['Value'][i]]) in v['Unnamed: 0'].unique(): _=tem['Count'][i]/100# 换算成公顷 Sum+=v[v['Unnamed: 0']==k].iloc[0,1:].sum()*_ # 总价值=\sum c_i*v_i return Sum dicT=defaultdict(int) tabs=os.listdir(path+r"\%sTabs"%year) total_esv=0 for i in tabs: tb=pd.read_excel(path+r"\%sTabs\\"%year+i) total_esv+=(k:=f(tb)) dicT[i.split("_")[0]]=k/1e10 return total_esv/1e10,dicT
Stable=[getValue(i) for i inrange(2017,2023)] DataShow=pd.DataFrame([],columns=[str(i) for i inrange(2017,2023)]) # 实际上,DF可以直接传入一个Dict,Key将作为Index,value将作为列值 for i inrange(2017,2023):DataShow[str(i)]=Stable[i-2017][1]
classMyDataSet(object): def__init__(self,seq,ws=6): # ws是滑动窗口大小 self.ori=[i for i in seq[:ws]] self.label=[i for i in seq[ws:]] self.reset() self.ws=ws