I am doing a Mann-Kendall analysis in which am iterating through a list of locations. I am trying to take the output from this analysis (defaults as a list but I convert it to a dataframe) and write it to an Excel Workbook as a unique Sheet (iterate through the locations to assign different sheet names).
Here is my code:
Note that pt is a dataframe with a time index and columns of concentrations of various metals at unique locations.
n=0
for x in pt.columns:
mkdata = mk.seasonal_test(pt[pt.columns[n]])
#Convert the output list from the Mann-Kendall analysis into a Data Frame
mktab = pd.DataFrame(mkdata)
#Renaming the first column from the Mann-Kendal to identify the location it is from
mktab.rename(columns={0:pt.columns[n]},inplace=True)
#Rename the Index to the parameters of the test results
mktab.rename(index={0: "Trend", 1: "h", 2: "p", 3: "z", 4: "Tau", 5: "s", 6: "var_s", 7: "Slope", 8: "Intercept"},inplace=True)
#Write this to Excel: Spoiler alert, this isn't working
with pd.ExcelWriter(oname) as writer:
开发者_JS百科 mktab.to_excel(writer,sheet_name='MK' + pt.columns[n])
#Iterate to the next column in the pt data frame
n=n+1
The problem is I get an output but it is only the last location in my list (I have 8 locations). Ideally I would like to have all of them as separate sheets. Or...even better, would be to merge them all in one dataframe that keeps increasing as the for loop iterates, but you can't have it all I suppose.
精彩评论