add read sell data
This commit is contained in:
		
							
								
								
									
										38
									
								
								read_data.py
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								read_data.py
									
									
									
									
									
								
							| @@ -5,6 +5,7 @@ import csv | |||||||
| sunlight_file_name = 'lightintensity.xlsx' | sunlight_file_name = 'lightintensity.xlsx' | ||||||
| factory_demand_file_name = 'factory_power1.xlsx' | factory_demand_file_name = 'factory_power1.xlsx' | ||||||
| electricity_price_data = 'electricity_price_data.csv' | electricity_price_data = 'electricity_price_data.csv' | ||||||
|  | electricity_price_data_sell = 'electricity_price_data_sell.csv' | ||||||
|  |  | ||||||
| df_sunlight = pd.read_excel(sunlight_file_name, header=None, names=['SunlightIntensity']) | df_sunlight = pd.read_excel(sunlight_file_name, header=None, names=['SunlightIntensity']) | ||||||
|  |  | ||||||
| @@ -26,41 +27,28 @@ print(df_power.head()) | |||||||
|  |  | ||||||
| df_combined = df_sunlight_resampled.join(df_power) | df_combined = df_sunlight_resampled.join(df_power) | ||||||
|  |  | ||||||
|  |      | ||||||
|  |  | ||||||
| df_combined.to_csv('combined_data.csv', index=True, index_label='Time') | price_df = pd.read_csv(electricity_price_data, index_col='Time', usecols=['Time', 'ElectricityBuy']) | ||||||
|  |  | ||||||
| def read_csv(file_path): |  | ||||||
|     return pd.read_csv(file_path, index_col='Time', usecols=['Time', 'ElectricityPrice']) |  | ||||||
|  |  | ||||||
| # price_data = np.random.uniform(0.3, 0.3, len(times)) |  | ||||||
|  |  | ||||||
| # 创建DataFrame |  | ||||||
| price_df = read_csv(electricity_price_data) |  | ||||||
| price_df.index = pd.to_datetime(price_df.index) | price_df.index = pd.to_datetime(price_df.index) | ||||||
| price_df = price_df.reindex(df_combined.index) | price_df = price_df.reindex(df_combined.index) | ||||||
|  |  | ||||||
| # price_df.set_index('Time', inplace=True) |  | ||||||
|  |  | ||||||
| # 保存到CSV文件 |  | ||||||
| # price_df.to_csv('electricity_price_data.csv', index=True) |  | ||||||
| print("price____") |  | ||||||
| print(price_df.index) |  | ||||||
| print("df_combined____") |  | ||||||
| print(df_combined.index) |  | ||||||
|  |  | ||||||
| print("Electricity price data generated and saved.") | print("Electricity price data generated and saved.") | ||||||
|  |  | ||||||
|  |  | ||||||
| df_combined2 = df_combined.join(price_df) | df_combined2 = df_combined.join(price_df) | ||||||
| print(df_combined2.head()) |  | ||||||
| # 保存结果 | sell_df = pd.read_csv(electricity_price_data_sell, index_col='Time', usecols=['Time', 'ElectricitySell']) | ||||||
|  | sell_df.index = pd.to_datetime(sell_df.index) | ||||||
|  | sell_df = sell_df.reindex(df_combined.index) | ||||||
|  |  | ||||||
|  | df_combined3 = df_combined2.join(sell_df) | ||||||
|  |  | ||||||
| with open('combined_data.csv', 'w', newline='') as file: | with open('combined_data.csv', 'w', newline='') as file: | ||||||
|     writer = csv.writer(file) |     writer = csv.writer(file) | ||||||
|     writer.writerow(['time', 'sunlight', 'demand','price']) |     writer.writerow(['time', 'sunlight', 'demand','buy', 'sell']) | ||||||
|     cnt = 0 |     cnt = 0 | ||||||
|     for index, row in df_combined2.iterrows(): |     for index, row in df_combined3.iterrows(): | ||||||
|         time_formatted = index.strftime('%H:%M') |         time_formatted = index.strftime('%H:%M') | ||||||
|         writer.writerow([time_formatted, row['SunlightIntensity'], row['FactoryPower'],row['ElectricityPrice']]) |         writer.writerow([time_formatted, row['SunlightIntensity'], row['FactoryPower'],row['ElectricityBuy'], row['ElectricitySell']]) | ||||||
|          |          | ||||||
|     print('The file is written to combined_data.csv') |     print('The file is written to combined_data.csv') | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user