import pandas as pd import os def normal(x): high = x.describe()['75%'] + 1.5*(x.describe()['75%']-x.describe()['25%']) low = x.describe()['25%'] - 1.5*(x.describe()['75%']-x.describe()['25%']) return x[(x<=high)&(x>=low)] fir_dir = './浙江各地市分电压日电量数据' qw_dir = 'C:\python-project\p1031\入模数据' result = pd.DataFrame({}) for excel,qw_excel in zip(os.listdir(fir_dir),os.listdir(qw_dir)): df_city = pd.read_excel(os.path.join(fir_dir,excel)) df_city = df_city[['stat_date','0.4kv及以下']] df_city['0.4kv及以下'] = df_city['0.4kv及以下']/10000 df_city = df_city.loc[normal(df_city['0.4kv及以下']).index] df_city['stat_date'] = df_city['stat_date'].map(lambda x:x.strip()) df_city['stat_date'] = pd.to_datetime(df_city['stat_date']) df_qw = pd.read_excel(os.path.join(qw_dir,qw_excel)) df_qw.columns = df_qw.columns.map(lambda x:x.strip()) df_qw = df_qw[['dtdate','tem_max','tem_min','holiday','24ST']] df_qw['dtdate'] = pd.to_datetime(df_qw['dtdate']) df = pd.merge(df_city,df_qw,left_on='stat_date',right_on='dtdate',how='left') df.drop(columns='dtdate',inplace=True) df.set_index('stat_date',inplace=True) list2 = [] list0 = [] list1 = [] for i in ('01','02','03','04','05','06','07','08','09','10','11','12'): month_index = df.index.strftime('%Y-%m-%d').str[5:7] == f'{i}' # print(df.loc[month_index]['0.4kv及以下'].max(),df['0.4kv及以下'].describe()['75%']) if df.loc[month_index]['0.4kv及以下'].mean() >= df['0.4kv及以下'].describe()['75%']: list2.append(i) elif df.loc[month_index]['0.4kv及以下'].mean() <= df['0.4kv及以下'].describe()['25%']: list0.append(i) else: list1.append(i) def season(x): if str(x)[5:7] in list0: return 0 elif str(x)[5:7] in list1: return 1 else: return 2 print(f'{excel[:2]}',list0) df['season'] = df.index.map(season) df.dropna(how='any',inplace=True) df.to_excel(f'./400v入模数据/{excel[:2]}.xlsx') # dict1 = {'杭州':0,'湖州':1,'嘉兴':2,'金华':3,'丽水':4,'宁波':5,'衢州':6,'绍兴':7,'台州':8,'温州':9,'舟山':10} # df['city'] = dict1[excel[:2]] # df.reset_index(inplace=True) # result = pd.concat([result,df])