import pandas as pd df = pd.read_excel(r'C:\Users\鸽子\Desktop\zj20231228.xlsx', sheet_name=2) df['stat_date'] = pd.to_datetime(df['stat_date']) # 移动平均 for city in df['city_name'].drop_duplicates(): print(city) df_city = df[df['city_name'] == city].set_index('stat_date').loc['2023-12'].sort_index() dict_big = {} dict_ok = {} resut_df = pd.DataFrame({}) index_industry = [] tq_list = [] pred_list = [] loss_list = [] rate_list = [] for industry in df_city.columns[2:]: # index_industry.append(industry) df_moving_avg = pd.DataFrame(df_city[industry], index=df_city.index) future = pd.date_range(start='2023-12-27', periods=5, freq='D') for date in future: df_moving_avg.loc[date, industry] = df_moving_avg[-3:].mean().values resut_df = pd.concat([resut_df, df_moving_avg], axis=1) print(city[4:6]) print(resut_df) # loss = (df_city1[industry].tail(-3).sum() - df_moving_avg.tail(-3).sum()) / df_city1[industry].sum() # tq_list.append(df_city1[industry].sum()) # pred_list.append(df_moving_avg[industry].sum()) # loss_list.append(df_city1[industry].sum()-df_moving_avg[industry].sum()) # rate_list.append((df_city1[industry].sum()-df_moving_avg[industry].sum())/df_city1[industry].sum()) with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\行业电量预测_1228.xlsx', mode='a', if_sheet_exists='replace', engine='openpyxl') as writer: resut_df.to_excel(writer, sheet_name=f'{city[4:6]}') # resut_df = pd.DataFrame({'同期电量':tq_list,'预测电量':pred_list,'偏差':loss_list,'偏差率':rate_list},index=index_industry) # print(resut_df) # resut_df.to_excel(r'C:\Users\鸽子\Desktop\移动平均_丽水_行业.xlsx') # if loss.values >= 0.005: # dict_big[industry] = loss.values[0] # else: # dict_ok[industry] = loss.values[0] # print(len(dict_ok)) # print(len(dict_big))