根據一張表中的數據自動填充另一張表


本程序的目標是根據姓名將來源表中的班級填至目標表中

來源表:

 

 

 目標表

 

 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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM