Chakot
Chakot

Reputation: 39

Update Pandas table in Tkinter after filtering

I'm new to Python. I have GUI with a Pandas table and a combobox (in the future more than one) to filter the table. What's the best way to update the table after filtered? Since I understand my question is basic, do you have any suggestion for a good course or book to use Pandastable in a Tkinter GUI? Please find attached my sample code Regards

import tkinter as tk
import pandas as pd
import numpy as np
from tkinter import ttk
from pandastable import Table

root = tk.Tk() 

frame1 = tk.Frame(root)
frame1.pack(fill='both',expand=True)
frame2 = tk.Frame(root)
frame2.pack()

# function to get unique values
def unique(list1):
    x = np.array(list1)
    print(np.unique(x))

df = pd.DataFrame({
    'logdate': ['1/1/2020','1/2/2022', '1/3/2022', '3/3/2021','5/13/2021','10/11/2019','1/5/2020'],
    'state': ['oh','oh', 'oh', 'ar','mi','ma','ms'],
    'provname': ['acme facility','123 healthcare', '123 healthcare','basic clinic','healthcare solutions','best clinic','one stop clinic'],
})

pt1 = Table(frame1, dataframe=df)
pt1.show()
# Create Combobox
n = tk.StringVar() 
combobox1 = ttk.Combobox(frame2, width = 27, textvariable = n) 
combobox1.pack() 
# Adding combobox drop down list 
combobox1['values'] = list(df['state'].unique())

def selection(event):
    #valore=combobox1.get()
    print('-------------')
    dfx = df.loc[(df['state'] == combobox1.get())]
    print(dfx)

combobox1.bind("<<ComboboxSelected>>", selection)

root.mainloop()

Upvotes: 1

Views: 388

Answers (1)

furas
furas

Reputation: 142641

You have to assign new dataframe to model in Table and redraw it

    pt1.model.df = dfx
    pt1.redraw()

Minimal working code

I added '- show all -' to show again all values.

import tkinter as tk
from tkinter import ttk
from pandastable import Table
import pandas as pd

# --- functions ---   # PEP8: all functions before main code

def selection(event):
    # you can get selected value in different ways
    #print(combobox.get())
    #print(n.get())
    #print(event.widget.get())
    
    selected = combobox.get()
    #selected = n.get()
    #selected = event.widget.get()
    
    if selected == '- show all -':
        dfx = df
    else:
        dfx = df[ df['state'] == selected ]
    
    print('---')
    print(dfx)
    
    # show filtered dataframe
    pt.model.df = dfx
    pt.redraw()
    
# --- main ---

df = pd.DataFrame({
    'logdate': ['1/1/2020', '1/2/2022', '1/3/2022', '3/3/2021', '5/13/2021', '10/11/2019', '1/5/2020'],
    'state': ['oh', 'oh', 'oh', 'ar', 'mi', 'ma', 'ms'],
    'provname': ['acme facility', '123 healthcare', '123 healthcare', 'basic clinic', 'healthcare solutions', 'best clinic', 'one stop clinic'],
})

root = tk.Tk() 

frame = tk.Frame(root)
frame.pack(fill='both', expand=True)

pt = Table(frame, dataframe=df)  # `Table` has to be in `Frame` but other widgets can be directly in `root`
pt.show()

n = tk.StringVar() 
combobox = ttk.Combobox(root, width=27, textvariable=n) 
combobox.pack()

combobox['values'] = ['- show all -'] + sorted(df['state'].unique())
combobox.bind("<<ComboboxSelected>>", selection)

root.mainloop()

PEP 8 -- Style Guide for Python Code


I don't know any course or book.

Long time ago I had to do something with PandasTable and some informations I found directly in source code.

I wrote some information in one post on my blog:

Tkinter PandasTable Examples [GB] — furas.pl

I found also some my answer on Stackoverflow:

pandas - Update table content using pandastable (Python, tkinter) - Stack Overflow

Upvotes: 2

Related Questions