You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.5 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# import pandas as pd
# import os
# import re
# file_dir1 = r'C:\Users\鸽子\Desktop\一版结果\电压等级电量预测结果\偏差率'
# file_dir2 = r'C:\Users\鸽子\Desktop\一版结果\电压等级电量预测结果\月底3天预测结果'
# file_dir3 = r'C:\Users\鸽子\Desktop\一版结果\行业电量预测结果\偏差'
# import numpy as np
# np.set_printoptions(threshold=np.inf)
#
# # print(os.listdir(file_dir3))
# # str1 = '丽水电压等级10kv以下月底偏差率:0.00229'
# #
# # print(re.split('电压等级|月底偏差率:',str1))
# # with open(os.path.join(file_dir3,'9月底偏差率.txt'),'r',encoding='utf-8') as f:
# # lines = f.readlines()
# # list_city = []
# # list_industry = []
# # list_loss = []
# # for i in lines:
# # i = re.split(':||其中', i)
# # print(i)
# # list_city.append(i[0][:2])
# # list_industry.append(i[-2].replace(i[0][:2],''))
# # list_loss.append(i[-1][:-2])
# # df_level = pd.DataFrame({'城市':list_city,'行业':list_industry,'偏差':list_loss})
# # # df_level.to_csv(os.path.join(file_dir3,'9月底偏差率.csv'),encoding='gbk')
# # print(df_level)
# file_dir = r'C:\python-project\pytorch3\浙江行业电量\浙江所有地市133行业数据'
# # print(os.listdir(file_dir))
# dict1 = {}
#
# for file in os.listdir(file_dir):
#
# df = pd.read_excel(os.path.join(file_dir,file),index_col=' stat_date ')
#
# col_list = df.drop(columns=[i for i in df.columns if (df[i] == 0).sum() / len(df) >= 0.5]).columns
# dict1[file[:2]] = col_list
# print(dict1)
#
# # print(len(df.drop(columns=[i for i in df.columns if (df[i] == 0).sum() / len(df) >= 0.5]).columns))
#
# read_path = r'C:\Users\鸽子\Desktop\一版结果\行业电量预测结果\月底预测结果'
# list1 = []
# for i in os.listdir(read_path):
# print(i)
# data = pd.read_csv(os.path.join(read_path, i), sep='\t',header=None)
# data = data[data.columns[1:]]
#
#
# for j,step in enumerate(range(0, len(data), 4)):
# df = data.iloc[step+1:step + 4, :]
# df.columns = ['预测值', '实际值', '偏差率']
# try:
# df['行业'] = dict1[i[2:4]][j]
# except:
# pass
# df['城市'] = i[2:4]
# list1.append(df)
# print(df)
# df = pd.concat(list1,ignore_index=True)
# df.to_csv('各市行业电量预测结果.csv',encoding='gbk')
# print(df)
import os
from openpyxl import Workbook
import pandas as pd
# df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江省11月分行业售电量预测v2.xlsx',sheet_name=1)
# print(df.head())
# print(df[df.columns[2:]].groupby(df['city_name']).sum().T)
# df2 = df[df.columns[2:]].groupby(df['city_name']).sum().T
# df2.to_excel(r'C:\Users\鸽子\Desktop\1.xlsx')
file_dir = r'C:\Users\鸽子\Desktop\11月区县分压预测'
for file in os.listdir(file_dir):
city = file[:-5]
wb = Workbook()
wb.save(fr'C:\Users\鸽子\Desktop\11月区县分压汇总\{city}.xlsx')
for file in os.listdir(file_dir):
city = file[:-5]
excel_file = pd.ExcelFile(os.path.join(file_dir,file))
sheet_names = excel_file.sheet_names[1:]
for sheet in sheet_names:
df = excel_file.parse(sheet)
df_result = df[df.columns[1:]].sum()
df_result = pd.DataFrame(df_result)
df_result.columns = ['售电量']
with pd.ExcelWriter(fr'C:\Users\鸽子\Desktop\11月区县分压汇总\{city}.xlsx', mode='a', engine='openpyxl',
if_sheet_exists='replace') as writer:
df_result.to_excel(writer, sheet_name=f'{sheet}')