根据excel 的第1列数据,从另一个excel表格取出与他相同的值。
将第二张图的B列与第一张图的B列的值相同的C列值copy到第一张图的C列
import pandas as pd
#两个cell相等,取出右边的单元格内容放入,src_arr,tar_arr两列 def get_target_value(src_arr,tar_arr): #源数据放入src_list,要查询的数据,得到的数据放入tar_list内 for i in range(len(src_arr)): for j in range(len(tar_arr)): if src_arr[i][0]==tar_arr[j][0]: tar_arr[j][1]=src_arr[i][1] return tar_arr #将数组写入excel
代码如下: if __name__=='__main__':
filename=r'D:\dwp_backup\python study\PDF\python tecnology competetion\exercise.xlsx' #read excies.xlsx df2=pd.read_excel(filename, header=None)
df_test=pd.read_excel(r'D:\dwp_backup\python study\PDF\New folder\dinkle_2ehdrrc-xxp - Copy.xlsx') #取出dataframe中的第一列和第二列组成数组array tar_arr=df_test.iloc[:,1:3].values src_arr=df2.iloc[:,1:3].values
get_target_value(src_arr,tar_arr)