Python Forum
Cleaning my code to make it more efficient
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cleaning my code to make it more efficient
#5
A quick and dirty (oh so dirty) example of using the filter idea above in a tkinter window that displays a dataframe.
import tkinter as tk
import pandas as pd
from dataclasses import dataclass
from random import choices


class Window(tk.Tk):
    def __init__(self, dataframe):
        super().__init__()
        self.df = dataframe
        self.columns = {}
        row = tk.Frame(self)
        row.pack(side=tk.TOP)
        for column in dataframe:
            col = tk.Frame(row)
            col.pack(side=tk.LEFT, padx=5, pady=5)
            tk.Label(col, text=column).pack()
            values = sorted(set(dataframe[column].values))
            var = tk.Variable(self, values)
            selector = tk.Listbox(
                col,
                width=10,
                height=10,
                listvariable=var,
                selectmode=tk.MULTIPLE,
                exportselection=False,
            )
            selector.pack()
            selector.bind("<<ListboxSelect>>", self.filter)
            selector.var = var
            self.columns[column] = selector
        self.table = tk.Label(self)
        self.table.pack(expand=True, fill=tk.BOTH)
        self.filter()

    def filter(self, *args):
        df = self.df.copy()
        for column, lbox in self.columns.items():
            if selection := lbox.curselection():
                choices = [lbox.get(index) for index in selection]
                df = df[df[column].isin(choices)]
        self.table["text"] = str(df)


# Make a dataframe for demonstrating filter.
letters = "ABCDEIOU"
numbers = [str(x) for x in range(1, 10)]
mixed = [f"{letter}{number}" for letter in letters for number in numbers]
df = pd.DataFrame(
    {
        "Letters": choices(letters, k=20),
        "Numbers": choices(numbers, k=20),
        "Mixed": choices(mixed, k=20),
    }
)

Window(df).mainloop()
Reply


Messages In This Thread
RE: Cleaning my code to make it more efficient - by deanhystad - Sep-26-2023, 08:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 892 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 750 Oct-22-2023, 09:08 PM
Last Post: tronic72
  A more efficient code titanif 2 529 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  how to make bot that sends instagram auto password reset code kraixx 2 1,472 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,187 Dec-03-2022, 10:07 PM
Last Post: Extra
  Making a function more efficient CatorCanulis 9 1,985 Oct-06-2022, 07:47 AM
Last Post: DPaul
  Apply textual data cleaning to several CSV files ErcoleL99 0 878 Jul-09-2022, 03:01 PM
Last Post: ErcoleL99
  Make the code shorter quest 2 1,570 Mar-14-2022, 04:28 PM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,863 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Pyspark - my code works but I want to make it better Kevin 1 1,850 Dec-01-2021, 05:04 AM
Last Post: Kevin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020