根据一张表中的数据自动填充另一张表

本程序的目标是根据姓名将来源表中的班级填至目标表中

来源表:

 目标表

 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)
原文地址:https://www.cnblogs.com/cxyrj/p/14332546.html