Tkinter Read file to create Pandas DataFrame


Tkinter Pandas DataFrame
Use the file browser to connect to any csv ( Comma Separated value ) file on click of a button. Use the read_csv() method to create a Pandas Dataframe by using the selected csv file.

After creation of Dataframe , display the number of rows and columns in the dataframe by using DataFrame attribute shape.
import pandas as pd
import tkinter  as tk 
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk()
my_w.geometry("400x300")  # Size of the window 
my_w.title('www.plus2net.com')

my_font1=('times', 12, 'bold')
l1 = tk.Label(my_w,text='Read File & create DataFrame',
    width=30,font=my_font1)  
l1.grid(row=1,column=1)
b1 = tk.Button(my_w, text='Browse File', 
   width=20,command = lambda:upload_file())
b1.grid(row=2,column=1) 
t1=tk.Text(my_w,width=40,height=5)
t1.grid(row=3,column=1,padx=5)

def upload_file():
    f_types = [('CSV files',"*.csv"),('All',"*.*")]
    file = filedialog.askopenfilename(filetypes=f_types)
    l1.config(text=file) # display the path 
    df=pd.read_csv(file) # create DataFrame
    str1="Rows:" + str(df.shape[0])+ "\nColumns:"+str(df.shape[1])
    #print(str1)
    t1.insert(tk.END, str1) # add to Text widget
my_w.mainloop()  # Keep the window open

Display DataFrame rows

DataFrame on Treeview
We will use Treeview to display rows of the DataFrame. Read this on how to create Headers and rows dynamically based on the different data sources.
import pandas as pd
import tkinter  as tk 
from tkinter import *
from tkinter import ttk
from tkinter import filedialog

my_w = tk.Tk()
my_w.geometry("500x350")  # Size of the window 
my_w.title('www.plus2net.com')

my_font1=('times', 12, 'bold')
lb1 = tk.Label(my_w,text='Read File & create DataFrame',
    width=30,font=my_font1)  
lb1.grid(row=1,column=1)
b1 = tk.Button(my_w, text='Browse File', 
   width=20,command = lambda:upload_file())
b1.grid(row=2,column=1,pady=5) 
lb2=tk.Label(my_w,width=40,text='',bg='lightyellow')
lb2.grid(row=3,column=1,padx=5)
l1=[] # List to hold headers of the Treeview 
def upload_file():
    global df ,l1
    f_types = [('CSV files',"*.csv"),('All',"*.*")]
    file = filedialog.askopenfilename(filetypes=f_types)
    lb1.config(text=file) # display the path 
    df=pd.read_csv(file) # create DataFrame
    l1=list(df) # List of column names as header 
    str1="Rows:" + str(df.shape[0])+ " , Columns:"+str(df.shape[1])
    #print(str1)
    lb2.config(text=str1) # add to Text widget
    trv_refresh() # show Treeview 
	
def trv_refresh(): # Refresh the Treeview to reflect changes
    global df,trv,l1 
    r_set=df.to_numpy().tolist() # create list of list using rows
    trv=ttk.Treeview(my_w,selectmode='browse',height=10,
        show='headings',columns=l1)
    trv.grid(row=4,column=1,columnspan=3,padx=10,pady=20)
    
    for i in l1:
        trv.column(i,width=90,anchor='c')
        trv.heading(i,text=str(i))
    for dt in r_set:
        v=[r for r in dt]
        trv.insert("",'end',iid=v[0],values=v)
my_w.mainloop()  # Keep the window open

Integrating Vertical Scroll bar

Here the trv_refresh() function is displaying the Treeview, in the same Treeview we will add one vertical scrollbar by adding this code inside the trv_refresh() function.
def trv_refresh(): # Refresh the Treeview to reflect changes
    global df,trv,l1 
    r_set=df.to_numpy().tolist() # create list of list using rows
    trv=ttk.Treeview(my_w,selectmode='browse',height=10,
        show='headings',columns=l1)
    trv.grid(row=4,column=1,columnspan=3,padx=10,pady=20)
    
    for i in l1:
        trv.column(i,width=90,anchor='c')
        trv.heading(i,text=str(i))
    for dt in r_set:
        v=[r for r in dt]
        trv.insert("",'end',iid=v[0],values=v)
        
    vs = ttk.Scrollbar(my_w,orient="vertical", command=trv.yview)
    trv.configure(yscrollcommand=vs.set)  # connect to Treeview
    vs.grid(row=4,column=4,sticky='ns') # Place on grid 
Here we have created the DataFrame by using local file and displaying, we can also search dataframe based on user inputs and then display the matching or resultant dataframe.
Part I : Searching DataFrame and displaying result in Treeview
Part III : Selection Columns of DataFrame using CheckButtons
Projects in Tkinter
Create Pandas DataFrame by reading Google Analytics csv file from Tkinter GUI Search DataFrame by user inputs through Tkinter.
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    06-06-2023

    hello,
    how to delete 1st list ?
    when i trying to import 2nd file/list, the label just staked

    22-07-2023

    The treeview is created every time you select a new file. So the previous data is removed and fresh data appers. Just check are you creating the treeview inside the function trv_refresh() or not.

    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer