本程序的目標是根據姓名將來源表中的班級填至目標表中
來源表:
目標表
1 import pandas as pd 2 import easygui as g 3 4 # 本程序的目標是根據姓名將來源表中的班級填至目標表中 5 lj1 = g.fileopenbox(msg='請選擇需要填充的excel表', title='選擇目標', default='c:\\Python37\\', filetypes=['*.xls', '*.xlsx']) 6 lj2 = g.fileopenbox(msg='請選擇來源的excel表', title='選擇來源', default='c:\\Python37\\', filetypes=['*.xls', '*.xlsx']) 7 df1 = pd.read_excel(lj1) 8 df2 = pd.read_excel(lj2) 9 10 # line_num 來源表中共有多少人 11 line_num = len(df1) 12 key = g.enterbox('請輸入關聯字段所在的列名') 13 tc_col = g.enterbox('請輸入需要填充的列名') 14 15 for m in range(line_num): 16 name = df1.loc[m,key] 17 # 求來源表中的姓名在目標表中哪些行中 18 hh = df2.loc[df2[key] == name].index 19 for i in range(len(hh)): 20 df2.loc[hh[i], tc_col] = df1.loc[m,tc_col] 21 22 print(df2)