original_excel_path :气象数据
station_info_path:站点数据
目标,merge数据和站点,加上经度纬度和海拔高度
(批量,每个original sheet)
02各sheet加经纬度.py
import pandas as pd
# Replace with your actual file paths
original_excel_path = 'original_excel.xlsx'
station_info_path = 'station_info.xlsx'
output_excel_path = 'output_excel.xlsx'
# Read the station info Excel file from Sheet1
station_info_df = pd.read_excel(station_info_path, sheet_name='Sheet1')
# Function to update station data in each sheet
def update_station_data(sheet_df):
# Merge the sheet data with station info based on '站号'
merged_df = pd.merge(sheet_df, station_info_df, how='left', left_on='站号', right_on='站号')
return merged_df
# Read each sheet from the original Excel file, update station data, and save to a new Excel file
with pd.ExcelWriter(output_excel_path, engine='xlsxwriter') as writer:
# Iterate over sheets in the original Excel file
for sheet_name, sheet_df in pd.read_excel(original_excel_path, sheet_name=None).items():
# Update station data for each sheet
updated_sheet_df = update_station_data(sheet_df)
# Save the updated data to a new sheet
updated_sheet_df.to_excel(writer, sheet_name=sheet_name, index=False)
# Print a message
print(f"Data has been saved to {output_excel_path}")
还要去掉降水或气温任意一个为-99999的站点,进一步修改代码
import pandas as pd
# Replace with your actual file paths
original_excel_path = 'original_excel.xlsx'
station_info_path = 'station_info.xlsx'
output_excel_path = 'output_excel.xlsx'
# Read the station info Excel file from Sheet1
station_info_df = pd.read_excel(station_info_path, sheet_name='Sheet1')
# Function to update station data in each sheet
def update_station_data(sheet_df):
# Merge the sheet data with station info based on '站号'
merged_df = pd.merge(sheet_df, station_info_df, how='left', left_on='站号', right_on='站号')
# Remove rows where '降水量' or '平均气温' is -99999
merged_df = merged_df[(merged_df['降水量'] != -99999) & (merged_df['平均气温'] != -99999)]
return merged_df
# Read each sheet from the original Excel file, update station data, and save to a new Excel file
with pd.ExcelWriter(output_excel_path, engine='xlsxwriter') as writer:
# Iterate over sheets in the original Excel file
for sheet_name, sheet_df in pd.read_excel(original_excel_path, sheet_name=None).items():
# Update station data for each sheet
updated_sheet_df = update_station_data(sheet_df)
# Save the updated data to a new sheet
updated_sheet_df.to_excel(writer, sheet_name=sheet_name, index=False)
# Print a message
print(f"Data has been saved to {output_excel_path}")