import pandas as pd df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江power1225.xlsx', sheet_name=1) df['pt_date'] = pd.to_datetime(df['pt_date']) # 移动平均 dict_big = {} dict_ok = {} for city in df['city_name'].drop_duplicates(): df_city1 = df[(df['city_name'] == city) & (df['county_name'].isnull())].set_index('pt_date').loc['2023-12'] resut_df = pd.DataFrame({}) index_level = [] tq_list = [] pred_list = [] loss_list = [] rate_list = [] for level in df_city1.columns[2:]: # index_level.append(level) df_moving_avg = pd.DataFrame(df_city1[level], index=df_city1.index) future = pd.date_range(start='2023-12-26', periods=5, freq='D') for date in future: df_moving_avg.loc[date, level] = 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[level].tail(-3).sum() - df_moving_avg.tail(-3).sum()) / df_city1[level].sum() # tq_list.append(df_city1[level].sum()) # pred_list.append(df_moving_avg[level].sum()) # loss_list.append(df_city1[level].sum() - df_moving_avg[level].sum()) # rate_list.append((df_city1[level].sum() - df_moving_avg[level].sum()) / df_city1[level].sum()) # resut_df = pd.DataFrame({'同期电量': tq_list, '预测电量': pred_list, '偏差': loss_list, '偏差率': rate_list}, # index=index_level) # resut_df = pd.DataFrame({'预测电量': pred_list},index=index_level) with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市分压电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', engine='openpyxl') as writer: resut_df.to_excel(writer, sheet_name=f'{city[4:6]}') # excel_file = pd.ExcelFile(r'C:\Users\鸽子\Desktop\11月移动平均分压.xlsx') # df1 = pd.read_excel(excel_file, sheet_name=1) # df1.set_index(df1.columns[0], inplace=True) # for sheet in excel_file.sheet_names[2:]: # df = pd.read_excel(excel_file, sheet_name=sheet) # df.set_index(df.columns[0], inplace=True) # df1 += df # df1['偏差'] = df1['同期电量'] - df1['预测电量'] # df1['偏差率'] = df1['偏差'] / df1['同期电量'] # df1.to_excel('移动平均_11月分压汇总.xlsx') # print(df1)