[英]How to use a column from one workbook, iterate through another and copy the corresponding data over
I have two excel workbooks.我有两个 excel 工作簿。 One is a list of names (10-15k) in column A and 1-3 digit number in column B. The second only has a list of names in column A. I want to upload both files then iter the names from workbook2 through workbook1 and receive workbook3 that would only have names that did not appear in workbook1 with a 0 in the next column over.一个是 A 列中的名称列表(10-15k)和 B 列中的 1-3 位数字。第二个只有 A 列中的名称列表。我想上传两个文件,然后从 workbook2 到 workbook1 迭代名称并接收工作簿 3,该工作簿 3 的名称仅出现在工作簿 1 中,下一列中为 0。 I'm trying to do this with tkinter so it can be an executable program for some coworkers.我正在尝试使用 tkinter 执行此操作,因此它可以成为某些同事的可执行程序。
I'm pretty new at coding so I'm getting a bit lost.我对编码很陌生,所以我有点迷路了。 What I have so far is below and I'm sure there are mistakes:(到目前为止我所拥有的如下,我确信有错误:(
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 400, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()
label1 = tk.Label(root, text='Interaction Comparison', bg = 'lightsteelblue2')
label1.config(font=('bookman old style', 20))
canvas1.create_window(200, 40, window=label1)
def getCSV1 ():
global read_file1
import_file_path = filedialog.askopenfilename()
read_file = pd.read_csv (import_file_path)
browseButton_CSV1 = tk.Button(text=" Import Interaction CSV File ", command=getCSV1, bg='green', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 110, window=browseButton_CSV1)
def getCSV2 ():
global read_file2
import_file_path = filedialog.askopenfilename()
read_file = pd.read_csv (import_file_path)
browseButton_CSV2 = tk.Button(text=" Import Comparison CSV File ", command=getCSV2, bg='green', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 160, window=browseButton_CSV2)
def convertToCSV ():
global read_file1, read_file2
data = pd.read_csv
for i in read_file1.data.iterrows(read_file2):
print (i)
export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
read_file.to_csv (export_file_path, index = None, header=True)
saveAsButton_CSV = tk.Button(text='CSV Comparison', command=convertToCSV, bg='green', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 210, window=saveAsButton_CSV)
def exitApplication():
MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
if MsgBox == 'yes':
root.destroy()
exitButton = tk.Button (root, text=' Exit Application ',command=exitApplication, bg='brown', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 260, window=exitButton)
root.mainloop()
Figured out what my issue was.弄清楚我的问题是什么。 Thank you to Hietsh Kumar for the reference to the merge method!感谢Hietsh Kumar对合并方法的参考!
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 400, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()
label1 = tk.Label(root, text='Interaction Comparison', bg = 'lightsteelblue2')
label1.config(font=('bookman old style', 20))
canvas1.create_window(200, 40, window=label1)
def getExcel1 ():
global df1
import_file_path = filedialog.askopenfilename()
df1 = pd.read_excel (import_file_path)
browseButton_CSV1 = tk.Button(text=" Import Interaction Excel File ", command=getExcel1, bg='green', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 110, window=browseButton_CSV1)
def getExcel2 ():
global df2
import_file_path = filedialog.askopenfilename()
df2 = pd.read_excel (import_file_path)
browseButton_CSV2 = tk.Button(text=" Import Comparison Excel File ", command=getExcel2, bg='green', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 160, window=browseButton_CSV2)
def combinetoExcel ():
df = pd.merge(df1,df2[['Alias','Interactions']], on = 'Alias', how = 'left')
df.to_excel('List.xlsx', index = False)
saveAsButton_CSV = tk.Button(text='Excel List', command=combinetoExcel, bg='green', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 210, window=saveAsButton_CSV)
def exitApplication():
MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
if MsgBox == 'yes':
root.destroy()
exitButton = tk.Button (root, text=' Exit Application ',command=exitApplication, bg='brown', fg='white', font=('bookman old style', 12, 'bold'))
canvas1.create_window(200, 260, window=exitButton)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.