python - 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()
解决方案
推荐阅读
- java - Hibernate - 具有接口属性的实体类
- amazon-web-services - 从临时凭证正确生成 AWS auth 标头
- php - PHP 意外的“内容”
- python-3.x - 在python中计算confusion_matrix
- java - 如何在 javaFX 动画期间删除以前的样式
- python - 将数字文件读入python中的元组?
- javascript - 如何使用 Vue.js 从 $emit 有效负载中获取值
- xml - 如何在 XSLT 中重复元素
- javascript - 我应该使用 JSX 条件在用户组件上显示管理功能吗?
- r - 以毫秒为单位将时间转换为 POSIX 以进行时间序列分析