![]() L1=tk.Label(my_w,text="Search", width=5,font=18)Į1=tk.Entry(my_w,width=15,bg='yellow',font=font1)ī1 = tk.Button(my_w, text="Save Result",bg='lightgreen',ĭf2 = df.iloc # create dataframe of same structure or columns Note that we have used df2 as global variable as it is used inside different functions.įull code : Copy to clipboard import pandas as pdįrom tkinter import filedialog # to show file dialog box for savingĭf=pd.read_excel("E:\\data\\student.xlsx") # create dataframe Here our DataFrame df2 is the filtered output so we will use to_excel() to save the DataFrame in Excel format as per the selected path. ![]() User can browse the local directory system and enter the file name to save at desired location. ![]() We can save the filtered output by using asksaveasfile dialog. #df2=df2.append(df,ignore_index=True)ĭf2=pd.concat(]) Save the output or result In place of append() we can use concat() as we may get this warning.įutureWarning: The frame.append method is deprecated and will be removedįrom pandas in a future version. Display the dataframe in Treeview.ĭf2=df2.drop_duplicates() # remove duplicate rows Using concat() Loop through all words and append to filtered dataframe df2 by using contains() string method.ĭf2=df2.append(df,ignore_index=True)įrom the final dataframe df2 remove the duplicate rows if any. Words=list(set(words)-set(words_stop)) # final list of words to search Words_stop= # List of words to be removed from searching To remove some words or not to consider a list of words for searching. Words=query.split(' ') # List of words by breaking a string. (remove the negative words from the list if required. Searching multiple words in DataFrame and display results in Treeview Part IIIīreak the input string using split() and one list is created with each words. My_w.mainloop() Without using search button Trv.insert("", "end", iid=v, values=v) # adding row R_set = df2.to_numpy().tolist() # Create list of list using rows Trv.insert("", "end", iid=v, values=v) # adding row Full code : Copy to clipboard import pandas as pdĭf = pd.read_excel("F:\\data\\student.xlsx") # create DataFrame Using this we will insert each row to our Treeview trv. Trv.heading(i, text=i) Inserting data rows to Treeviewįrom our resulted DataFrame we have collected the list of rows as r_set. We will first display the headers by looping through the list l1.ĭynamic Creation of Header & Columns in Treeview » trv = l1 Previously we have collected all column headers as a list l1 from the source dataframe df. Trv = 10 # Number of rows to display, default is 10 Trv = ttk.Treeview(my_w, selectmode="browse") # selectmode="browse" or "extended" Here the final output of the query or the resulted dataframe is displayed using the Treeview. Inside the function my_search() we will use Tkinter Treeview to display Tabular data. R_set = df2.to_numpy().tolist() # Create list of list using rows Creating Treeview Str1 = df.(query, case=False) # name column value matchingĭf2 = df # combine all conditions using | operator Query = e1.get().strip() # get user entered string L1 = list(df) # List of column names as headers We used tolist() to create a list from the DataFrame If the user input is not a digit then we will use string method contains() to match against the name column of the source DataFrame df. We will read the user inputs in entry widget e1 and then remove the space from both sides by using strip(). L1.grid(row=1, column=1, padx=3, pady=10)Į1 = tk.Entry(my_w, width=35, bg="yellow", font=18) # added one Entry boxī1 = tk.Button(my_w, text="Search", width=7, font=18, command=lambda: my_search())įirst we will create a list of column names or headers from the main dataframe df. L1 = tk.Label(my_w, text="Search", width=5, font=18) # added one Label My_w.geometry("500x350") # width x height We will keep one entry widget e1 to collect the user input and on click of the Button b1 we will call the function my_search(). Tkinter interface to search and filter Pandas DataFrame and display rows in Treeview
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |