用到的函数:
mk.original_test
计算各种指标.# coding:utf-8
import pandas as pd
import pymannkendall as mk
"""
Author:俊哥的地理空间 Date:2022/12/30
excel数据样例:
2000 11 20 15
2001 12 19 18
2002 13 18 20
2003 14 17 22
2004 15 16 24
2005 16 15 22
2006 17 14 20
2007 18 13 18
2008 19 12 16
2009 20 11 14
2010 21 10 12
"""
data = pd.read_excel('D:/data.xlsx')
rows = data.shape[0]
cols = data.shape[1]
print(rows,cols)
for i in range(1,cols):
r = data.iloc[:,i] # 循环读取每一列数据
res = mk.original_test(r)
print(res)
print(f'trend:{res.trend}','p_value:{:.2f}'.format(res.p),'slope:{:.2f}'.format(res.slope),sep = ',')
D:\Python3.9\python.exe D:/PyCode/applyscript/MK趋势分析Excel.py
10 4
Mann_Kendall_Test(trend='increasing', h=True, p=8.303070332638107e-05, z=3.9354796403996297, Tau=1.0, s=45.0, var_s=125.0, slope=1.0, intercept=12.0)
trend:increasing,p_value:0.00,slope:1.00
Mann_Kendall_Test(trend='decreasing', h=True, p=8.303070332638107e-05, z=-3.9354796403996297, Tau=-1.0, s=-45.0, var_s=125.0, slope=-1.0, intercept=19.0)
trend:decreasing,p_value:0.00,slope:-1.00
Mann_Kendall_Test(trend='decreasing', h=True, p=0.03731305320219125, z=-2.082322158977926, Tau=-0.5333333333333333, s=-24.0, var_s=122.0, slope=-1.3333333333333333, intercept=25.0)
trend:decreasing,p_value:0.04,slope:-1.33
Process finished with exit code 0
终于出羊圈了,不过还有一点点咳嗽…
更多代码和软件操作可以关注个人公粽号:俊哥的地理空间 欢迎留言交流