Clear a Data Table for a new data file

If I over-write a Data Table with new data using a read-csv function, the anvil data table seems to keep the old data columns even if they don’t exist in the new data set (although the show greyed out with none as the data).

I see there is a command to delete all rows of a data table, but how do I completely clear the table columns and rows of data and start fresh with a new data table for the next data file to be loaded into?

Hello and welcome.

There is something like:
my_table.delete_all_rows() # may want to search in case I've got this slightly wrong

I do not believe there is a command to delete the columns in a data table. Feature requests are welcome though if that is the case.

You might want to add a like to this feature request:

OK, thanks. I guess maybe what I can do is keep adding tables for each time a new file is loaded. But then at some point I would want a way to clean up the old tables that are no longer needed.

This Webcomic is not all that helpful for your situation, but it is relevant to this particular conversation, and also funny.

1 Like

You could just create a single download_csv table and then have normalized column names like Column1, Column2, etc…Then just append the previous table names to the first row. After each run you can delete the rows to reset the table. Here is some code to do this:

def normalize_col_names(table: list):
    # Convert into a DataFrame
    table_df = pd.DataFrame.from_records(table)

    # Get the columns
    columns = table_df.columns

    # Add the original columns as the first row
    header_row = {}
    for column in columns:
        header_row[column] = [column]

    header_row_df = pd.DataFrame(header_row)
    table_df_mod = pd.concat([header_row_df, table_df])

    # Create standard columns
    standard_columns = []
    for i in range(len(columns)):
        standard_columns.append("Column" + str(i))

    # Combine the columns into a mapping
    col_mapping = dict(zip(columns, standard_columns))

    # Rename the columns in the modified DataFrame
    table_df_mod.rename(col_mapping, inplace=True, axis=1)
    return table_df_mod.to_dict("records")