Fastest way to populate QTableView from Pandas data frame

后端 未结 7 1233
面向向阳花
面向向阳花 2020-11-27 16:54

I\'m very new to PyQt and I am struggling to populate a QTableView control.

My code is the following:

def data_frame_to_ui(self, data_frame):
                


        
7条回答
  •  無奈伤痛
    2020-11-27 17:18

    Simple and faster way to write a dataframe to QtableWidget

    # Takes a df and writes it to a qtable provided. df headers become qtable headers
    @staticmethod
    def write_df_to_qtable(df,table):
        headers = list(df)
        table.setRowCount(df.shape[0])
        table.setColumnCount(df.shape[1])
        table.setHorizontalHeaderLabels(headers)        
    
        # getting data from df is computationally costly so convert it to array first
        df_array = df.values
        for row in range(df.shape[0]):
            for col in range(df.shape[1]):
                table.setItem(row, col, QtGui.QTableWidgetItem(str(df_array[row,col])))
    

提交回复
热议问题