简体   繁体   English

如何使用一个工作簿中的列,遍历另一个工作簿并复制相应的数据

[英]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.

相关问题 如何使用 Python 从一个 excel 复制数据并打印到另一个 excel 的相应行 - how to use Python to copy data from one excel and print into corresponding row in another excel 使用openpyxl将单元格值列从一个工作簿复制到另一个工作簿 - Copy column of cell values from one workbook to another with openpyxl 如何使用 openpyxl 将工作表从一个工作簿复制到另一个工作簿? - How to copy worksheet from one workbook to another one using openpyxl? 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 将 Excel 行从一个工作簿复制到另一个工作簿 - Copy Excel rows from one workbook to another workbook 如何遍历每个数据条目并从另一个中减去每个条目? [Python] - How to iterate over every data entry and subtract each one from another? [Python] 如何在python中遍历数据框中的一列,得到该行对应的算术计算 - How to iterate through a column in data frame in python and get the arithmetic calculation corresponding to the row 将行从一本工作簿复制到具有特定列值的Excel中 - Copy the rows from one workbook to another in Excel having specific column value 从一个数据框中复制数据并将与该值对应的列名粘贴到新数据框中 - Copy data from one data frame and paste in column name corresponding to that value in a new Data Frame pandas df 迭代一个 df 列 qty 并以 fifo 为基础从另一个 df 列分配 qty - pandas df iterate over one df column qty and allocate qty from another df column with fifo basis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM