import tkinter as tk
from tkinter import ttk
import pandas as pd
# Sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [24, 30, 22],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
def display_dataframe(df):
root = tk.Tk()
root.title("Pandas DataFrame")
frame = ttk.Frame(root)
frame.pack(fill='both', expand=True)
tree = ttk.Treeview(frame, columns=list(df.columns), show='headings')
tree.pack(side='top', fill='both', expand=True)
# Add headings
for col in df.columns:
tree.heading(col, text=col)
# Add data rows
for index, row in df.iterrows():
tree.insert("", "end", values=tuple(row))
# Add scrollbar
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.pack(side='right', fill='y')
root.mainloop()
# Display the DataFrame
display_dataframe(df)
You can replace the sample data with your own DataFrame. This could be loaded from a CSV file or any other data source supported by Pandas. The script initializes a Tkinter window, sets up a table with headings, and populates it with data from the DataFrame.