Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从Panda获取Treeview的行数据_Python 3.x_Pandas_Numpy_Tkinter_Treeview - Fatal编程技术网

Python 3.x 从Panda获取Treeview的行数据

Python 3.x 从Panda获取Treeview的行数据,python-3.x,pandas,numpy,tkinter,treeview,Python 3.x,Pandas,Numpy,Tkinter,Treeview,我有一个.xlsx文件,我想使用TKinter的Treeview在GUI中重新创建该表。下面我有一个解决方案,可以提供我想要的输出,但它很长,我不确定是否有更好的方法来实现。对于我的应用程序来说,性能是一个值得关注的问题,因为我没有那么多的功能,我认为使用更大的.xlsx文件,我将开始看到性能的下降 我还必须假设我不知道标题和行数,但列数小于15 import numpy as np import pandas as pd xls = pd.ExcelFile('fil

我有一个
.xlsx
文件,我想使用TKinter的Treeview在GUI中重新创建该表。下面我有一个解决方案,可以提供我想要的输出,但它很长,我不确定是否有更好的方法来实现。对于我的应用程序来说,性能是一个值得关注的问题,因为我没有那么多的功能,我认为使用更大的
.xlsx
文件,我将开始看到性能的下降

我还必须假设我不知道标题和行数,但列数小于15

    import numpy as np
    import pandas as pd

    xls = pd.ExcelFile('file.xlsx');
    sheetData = pd.read_excel(xls, 'Sheet-1')

    # Get column headings
    headings = sheetData.columns

    # Convert headings to list
    data = list(headings.values.tolist())

    # Get row count
    rows = len(sheetData)

    # Create tree with the the number of columns
    # equal to the sheet, the id of the column 
    # equal to the column header and disable
    # the 'treeview'
    tree = ttk.Treeview(self, columns=data, show=["headings"],selectmode='browse')


    # Create column headings on tree
    for heading in headings: 
        heading = str(heading) # convert to string for processing
        tree.column(heading, width=125, anchor='center')
        tree.heading(heading, text=heading)

    # Populate rows --The part that concerns me
    for rownumber in range(rows):
        rowvalue = sheetData.values[rownumber]      # Get row data 
        rowvalue = np.array2string(rowvalue)        # Convert from an np array to string
        rowvalue = rowvalue.strip("[]")             # Strip the string of square brackets
        rowvalue = rowvalue.replace("'",'')         # Replace all instances of ' with no character
        tree.insert('', 'end', values= rowvalue)    # Append the row to table

有没有更简单的方法来获取行数据并将其附加到树视图中?

我创建了一个简单的示例来实现这一点:

import tkinter as tk
from tkinter import ttk
import pandas as pd

# Maybe This is what you want 
def Start():
    fp = pd.read_excel("./test.xlsx") # Read xlsx file
    for _ in range(len(fp.index.values)): # use for loop to get values in each line, _ is the number of line.
        tree.insert('','end',value=tuple(fp.iloc[_,[1,2]].values)) # [_,[1,2]] represents that you will get the values of second column and third column for each line.


win = tk.Tk()
win.wm_attributes('-topmost',1)
# win.geometry("+1300+0")

ttk.Button(win,text="Import it",command=Start).pack()

columns = ("name","gender")

tree = ttk.Treeview(win,show="headings",columns=columns)
tree.column("name",width=100,anchor='center')
tree.column("gender",width=50, anchor="center")

tree.heading("name",text="name")
tree.heading("gender",text="gender")
tree.pack()
win.mainloop()
这是我的示例excel文件:

结果: