首页 > 解决方案 > Python 类和 Def 组织 - Python pandas 和 tkinter

问题描述

我是 python pandas/tkinter 的新手,并且无法按类/def 函数分组。我现在的目标是将所有代码分组,以便在按下按钮时执行并导出新的 CSV 文件(变量名为 new_df)。请帮我在下面对这段代码进行分组,以便在单击按钮后我可以在命令 tkinter 消息框中导出 CSV。谢谢!

目标:按下按钮后运行代码。:-)

import tkinter.messagebox
import pandas as pd
import datetime

class winners:
    df = pd.read_csv(r"C:\Users\D\OneDrive\Desktop\data_1.csv") #to read a CSV
    df.head()

    #finding Tuesday date:
    def End_Tuesday(d, weekday):
        days_ahead = weekday - d.weekday()
        if days_ahead <= 0: #Target day already happened this week
            days_ahead -= 3
        return d + datetime.timedelta(days_ahead)


    d = datetime.date.today()
    Tuesday = End_Tuesday(d, 1) # 0 = Monday, 1=Tuesday, 2=Wednesday...
    New_Tuesday = Tuesday.strftime('%m/%d/%y') #convert to string
    print(New_Tuesday + ' 24:00') #print Tuesday date --> works if data is extracted on Friday only!!!!

    #exporting Date within timeframe
    df['DATE'] = pd.to_datetime(df['DATE'], format='%m/%d/%Y %H:%M')
    start_date = '05-24-2021 12:00'
    #end_date = '05-26-2021 21:00'
    end_date = New_Tuesday
    mask = (df['DATE'] > start_date) & (df['DATE'] <= end_date)
    new_df = df.loc[mask]

    print(new_df)

    path = '/Users/file/Desktop' #for exporting to new csv
    new_file = 'New_file.csv'
    new_df.to_csv(new_file, index = False) #index = False is no index to csv

#tkinter message box 
top = tkinter.Tk()

def helloCallBack():
   tkinter.messagebox.showinfo( "Hello Python", "Hello World")

B = tkinter.Button(top, text ="Exporting winners CSV file to desktop...", command = winners) #command = ???

B.pack()
top.mainloop()

标签: pythonpandasfunctionclasstkinter

解决方案


推荐阅读