import pandas as pd import numpy as np import csv sunlight_file_name = 'lightintensity.xlsx' factory_demand_file_name = 'factory_power1.xlsx' 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']) start_date = '2023-01-01 00:00:00' # 根据数据的实际开始日期调整 hours = pd.date_range(start=start_date, periods=len(df_sunlight), freq='h') df_sunlight['Time'] = hours df_sunlight.set_index('Time', inplace=True) df_sunlight_resampled = df_sunlight.resample('15min').interpolate() df_power = pd.read_excel(factory_demand_file_name, header=None, names=['FactoryPower'], dtype={'FactoryPower': float}) times = pd.date_range(start=start_date, periods=len(df_power), freq='15min') df_power['Time'] = times df_power.set_index('Time',inplace=True) print(df_power.head()) df_combined = df_sunlight_resampled.join(df_power) price_df = pd.read_csv(electricity_price_data, index_col='Time', usecols=['Time', 'ElectricityBuy']) price_df.index = pd.to_datetime(price_df.index) price_df = price_df.reindex(df_combined.index) print("Electricity price data generated and saved.") df_combined2 = df_combined.join(price_df) 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: writer = csv.writer(file) writer.writerow(['time', 'sunlight', 'demand','buy', 'sell']) cnt = 0 for index, row in df_combined3.iterrows(): time_formatted = index.strftime('%H:%M') writer.writerow([time_formatted, row['SunlightIntensity'], row['FactoryPower'],row['ElectricityBuy'], row['ElectricitySell']]) print('The file is written to combined_data.csv') # combined_data.to_csv('updated_simulation_with_prices.csv', index=False) print("Simulation data with electricity prices has been updated and saved.")