过滤熊猫数据框,以tkinter / pandastable

时间:2018-12-16 17:45:39

标签: python pandas tkinter

我正在尝试创建一个涉及显示中型熊猫数据框的应用程序。我在用户界面中使用了pandastable和tkinter。我希望用户能够执行的操作的一部分是从组合框中选择一个过滤器,然后仅显示匹配的行。

下面是我尝试使用的代码的简化但完整的版本。我创建一个包含核心UI代码的类。最初,一切看起来都很不错。我在类(change_df)内创建了一个测试按钮函数,该函数创建了一个虚拟列,效果很好。我在类(change_df_combo)中有另一个函数可以成功过滤数据框,但不会在屏幕上显示。即,我可以打印df或将其导出并进行过滤。但是,这不会显示在屏幕上。

我认为我的班级或tkinter本身在某种程度上并不是正确的循环,但是对于我的一生,我无法弄清楚!有什么想法吗?

from win32com.shell import shell, shellcon
import pandas as pd
import openpyxl
from openpyxl import load_workbook
import numpy as np
import time
from tkinter import *
from tkinter import ttk
from pandastable import Table, TableModel

class UserInterface(Frame):
    # Launch the df in a pandastable frame
    def __init__(self, parent=None):
        global ui_df
        global pt
        ui_df = pos_df
        self.parent = parent
        Frame.__init__(self)
        self.main = self.master
        #self.main.geometry('800x600+0+0')
        f = Frame(self.main)
        f.grid(column=0, row=1, sticky=(E, W))
        screen_width = f.winfo_screenwidth() * 0.8
        screen_height = f.winfo_screenheight() * 0.7
        self.table = pt = Table(f, dataframe=ui_df, height = screen_height, width = screen_width)
        pt.show()
        return

    def change_df(self, col_val_input):
        #Responds to button
        ui_df['Test col'] = col_val_input
        pt.show()

    def change_df_combo(self, event):
        #Responds to combobox, supposed to filter by 'Sec_type'
        combo_selection = str(combo_box.get())
        ui_df = pos_df[pos_df['Sec_type'] == combo_selection]
        pt.show()

#Create dataframe
pos_data = {'Location' : ['Denver', 'Boulder', 'Phoenix', 'Reno', 'Portland',
'Eugene', 'San Francisco'], 'Sec_type' : ['mbus', 'mbus', 'vmus', 'caus',
'vmus', 'mbus', 'mbus']}
pos_df = pd.DataFrame(data = pos_data)

#Launch Tkinter basics
root = Tk()
root.title("S test...")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

ui_display = UserInterface(mainframe)

#'Test' button, this works fine
col_val_input = 'It worked!'
test_button = ttk.Button(mainframe, text = 'Test', command= lambda: ui_display.change_df(col_val_input))
test_button.grid(column=0, row=0, sticky=(W))

#Combobox, works to just show choice, but not to filter
combo_choices = ['mbus', 'vmus', 'caus']
choice = StringVar()
combo_box = ttk.Combobox(mainframe, textvariable=choice)
combo_box['values'] = combo_choices
combo_box.grid(column=1, row=0, sticky=(W))
combo_box.bind('<<ComboboxSelected>>', ui_display.change_df_combo)

root.mainloop()

编辑以添加: 出于测试目的,将导入的df更改为玩具df

1 个答案:

答案 0 :(得分:0)

好的,我想到了一些可行的方法。不确定它是否是有史以来编写的最多的pythonic代码,但是它在这里:(仅张贴类段)

SELECT * FROM all_countries 
LEFT JOIN supported_countries ON all_countries.id = supported_countries.id 
WHERE supported_countries.id IS NULL