python 类似excel的vlookup,根据相同名称列,把其中一个excel里的新列,放入到另一个excel里面


根据相同站名、日期、时把降水数据放到第二个excel里面
import pandas as pd # 读取两个Excel文件 cbh_df = pd.read_excel('D:/云物理量/CBH_时间对齐.xlsx') precipitation_df = pd.read_excel('D:/云物理量/8月10日实况降水.xlsx') # 保留原始日期格式,不进行任何转换 # 直接使用字符串形式创建合并键 cbh_df['合并键'] = cbh_df['站名'].astype(str) + '_' + cbh_df['日期'].astype(str) + '_' + cbh_df['对齐时间'].astype(str) precipitation_df['合并键'] = precipitation_df['站名'].astype(str) + '_' + precipitation_df['日期'].astype(str) + '_' + precipitation_df['时'].astype(str) # 创建一个字典,映射合并键到降水量 precip_dict = precipitation_df.set_index('合并键')['过去1小时降水量'].to_dict() # 将降水量映射到CBH数据 cbh_df['过去1小时降水量'] = cbh_df['合并键'].map(precip_dict) # 删除临时的合并键列 cbh_df.drop('合并键', axis=1, inplace=True) # 保存结果 cbh_df.to_excel('D:/云物理量/CBH_时间对齐_带降水量.xlsx', index=False) print("处理完成!")

浙公网安备 33010602011771号