skip to Main Content

I am looking for a solution to display a table in a window (separate from the console). I am using Ubuntu 22.04 system.

The data are processed by pandas.
I found this guide: https://www.delftstack.com/fr/howto/python-pandas/pandas-display-dataframe-in-a-table-style/
that the Styler object may display a table from a DataFrame.

Thus, I tried this:
Inside the code, there is a 2D list called "week". I would like to display it:

df = pd.DataFrame(week)

df.style

print(df)

But, nothing new happens after doing df.style. However, print(df) works fine.

3

Answers


  1. Have you tried using a Jupyter notebook? It’s useful for separate charts visualizations, so you could display your pd.DataFrame() in different cells, I’m assuming that is what you mean by display a dataframe in separate windows.

    Login or Signup to reply.
  2. Use this

    import pandas as pd
    import tkinter as tk
    from tkinter import ttk
    
    # Sample data
    week = [
        ['Monday', 'Math', 'English', 'History'],
        ['Tuesday', 'Science', 'Art', 'PE'],
        ['Wednesday', 'Math', 'Geography', 'French'],
        ['Thursday', 'Physics', 'Chemistry', 'Biology'],
        ['Friday', 'Math', 'English', 'History']
    ]
    
    # Create a DataFrame
    df = pd.DataFrame(week, columns=['Day', 'Subject 1', 'Subject 2', 'Subject 3'])
    
    # Function to display DataFrame in a Tkinter window
    def display_dataframe(df):
        root = tk.Tk()
        root.title("Pandas DataFrame")
    
        frame = ttk.Frame(root)
        frame.pack(fill=tk.BOTH, expand=True)
    
        # Create a treeview widget
        tree = ttk.Treeview(frame)
        tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    
        # Define columns
        tree["columns"] = list(df.columns)
        tree["show"] = "headings"
    
        # Create headings
        for col in df.columns:
            tree.heading(col, text=col)
    
        # Add data to treeview
        for index, row in df.iterrows():
            tree.insert("", "end", values=list(row))
    
        # Add a scrollbar
        scrollbar = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
        scrollbar.pack(side=tk.RIGHT, fill="y")
        tree.configure(yscrollcommand=scrollbar.set)
    
        root.mainloop()
    
    # Display the DataFrame in a Tkinter window
    display_dataframe(df)
    

    This approach provides a simple and effective way to visualize your DataFrame data outside the console on Ubuntu (or any other system that supports Tkinter).

    Login or Signup to reply.
  3. Any IDE can do it by default or with extensions, eg. VS Code, PyCharm, DataSpell…
    With VSC you can use default Data Viever other extensions eg. Data Wrangler (that uses venv with pandas)
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search