首页 > 解决方案 > Tkinter Scale 以将值输入到 pandas dataframe.head

问题描述

我正在尝试使用 Tkinter 的 Scale 小部件将值传递给应该修改 pandas dataframe.head() 值的函数。例如,如果 Tkinter 比例设置为 50,则传递给 .head 方法的值应如下所示:dataframe.head(50)。

我的代码如下所示:

def scatterplot(value):
    try:
        global graph
        global bar_chart_df
        global clean_df
        global correlation
        correlation = clean_df.groupby(['FACILITY ZIP', 'FACILITY NAME'])['VIOLATION DESCRIPTION'].count().reset_index(name='No. Violations').sort_values(by='No. Violations', ascending=False)
    
        hue = correlation["FACILITY NAME"].head(value)
        y = correlation["FACILITY ZIP"].head(value)
        x = correlation["No. Violations"].head(value)
    
        sns.scatterplot(x, y, hue)
        plt.title('Correlation between the number of violations committed per vendor and their zip code (sample of 150)')
        plt.legend(loc='upper right')
        plt.figure(figsize=(30, 45), dpi=300, facecolor='w', edgecolor='k')
        plt.tight_layout()
        plt.show()
     except:
        my_error = tk.messagebox.showerror(title="Try again", message="Please load a valid dataset")



cmd_frame_3 = ttk.LabelFrame(self.window, text="Statistics", relief=tk.RIDGE)
cmd_frame_3.grid(row=2, column=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=6)
    
my_scale = tk.Scale(entry_frame, from_=0, to=100, command=scatterplot, orient=tk.HORIZONTAL, width=8, length=200)
my_scale.grid(row=4, column=2, sticky=tk.W)

由于某种原因,它不起作用并显示此错误:

相关性 = clean_df.groupby(['FACILITY ZIP', 'FACILITY NAME'])['VIOLATION DESCRIPTION'].count().reset_index(name='No. Violations').sort_values(by='No. Violations',升序=假)

NameError:名称“clean_df”未定义

Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
文件“/opt/anaconda3/lib/python3.8/tkinter/init .py ”,第 1883 行, 调用中 返回 self.func(*args)

我没看到什么?

标签: pandasdataframematplotlibtkinter

解决方案


推荐阅读