An efficient way to fill a dearpygui table using pandas

1.2k Views Asked by At

For now, I just make each column in to a list df['Name'].to_list() -> zip(list1,list2 ,....) all the lists, and iterate over them and then I add them in the table.

I would imagine this is far from an ideal solution. Is there anything better to fill the dearpygui table while using pandas?

2

There are 2 best solutions below

0
On

Additional modification to Torko's answer, pandas dataframe iloc() is slow. use numpy array:

df = pd.read_csv(filename)                          # Take your df from wherever
arr = df.to_numpy()                                 ### Convert the DataFrame to a NumPy array.

with dpg.table(label='DatasetTable'):
    for i in range(df.shape[1]):                    # Generates the correct amount of columns
        dpg.add_table_column(label=df.columns[i])   # Adds the headers
    for i in range(df.shape[0]):                                   # Shows the first n rows
        with dpg.table_row():
            for j in range(df.shape[1]):
                dpg.add_text(f"{arr[i,j]}")     # Displays the value of
                                                         # each row/column combination
0
On

I don't know much about your approach but here is a generalized example of what i use:

dataset = pd.read_csv(filename)                          # Take your df from wherever

with dpg.table(label='DatasetTable'):
    for i in range(dataset.shape[1]):                    # Generates the correct amount of columns
        dpg.add_table_column(label=dataset.columns[i])   # Adds the headers
    for i in range(n):                                   # Shows the first n rows
        with dpg.table_row():
            for j in range(dataset.shape[1]):
                dpg.add_text(f"{dataset.iloc[i,j]}")     # Displays the value of
                                                         # each row/column combination

I hope it can be useful to someone.