[docs]defrender_dataframe(df):""" Prints the Pandas DataFrame in a human-readable format. Args: df (pandas.DataFrame): The DataFrame to be rendered. """# Calculate widthscol_widths=[_calculate_index_width(df)]+_calculate_column_widths(df)# Print header_print_row(_prepare_header(df),col_widths)# Print rowsforindex,rowindf.iterrows():_print_row([str(index)]+[str(row[col])forcolindf.columns],col_widths)
def_calculate_index_width(df):""" Calculates the width of the index column. Args: df (pandas.DataFrame): The DataFrame to be analyzed. Returns: int: The width of the index column. """returnmax(len(str(df.index.name)ifdf.index.nameelse""),df.index.astype(str).map(len).max(),)def_calculate_column_widths(df):""" Calculates the width of each column in the DataFrame. Args: df (pandas.DataFrame): The DataFrame to be analyzed. Returns: list: A list of column widths. """return[max(len(str(col)),df[col].astype(str).map(len).max())forcolindf.columns]def_prepare_header(df):""" Prepares the header row for the DataFrame. Args: df (pandas.DataFrame): The DataFrame to be analyzed. Returns: list: A list of header values. """return[df.index.nameor" "]+list(df.columns)def_print_row(values,widths):""" Prints a single row of the DataFrame. Args: values (list): A list of values to be printed in the row. widths (list): A list of column widths. """print(" ".join(f"{values[i]:<{widths[i]}}"foriinrange(len(values))))