import numpy as np
import pandas as pd
import codecs
path=r'C:\try.csv' #读取格点数据CSV
data=pd.read_csv(path,encoding='gbk')
outpath=r'C:\rate.csv'#新建存放斜率的CSV
file_csv = codecs.open(outpath,'w+','utf-8') #打开方式
data1=data.T #转置
n=80 #格点数
m=17 #年数
sl=[] #空列表
for i in list(range(0,80)):#循环计算80个格点的变化斜率
print (i)
sp=[]
y=data1[i]
y=y.values
for k in range(0,m-1):
for j in range(k+1,m):
print(k,j)
s=(y[j]-y[k])/(j-k) #计算斜率
sp.append(s)
sl.append(np.median(sp))
slop= pd.DataFrame(sl)
slop.to_csv(outpath,header=['slop']) #将SLOP写入CSV