How to Customize Column Widths in Python tabulate
The tabulate library in Python allows you to create formatted tables. One of its key features is the ability to customize column widths.
In this tutorial, you’ll learn various methods to adjust column widths in tabulate, from simple fixed-width solutions to more dynamic methods.
Set a single width for all columns
To set the width for all columns, you can use the maxcolwidths
parameter in the tabulate function.
You can create a simple table with fixed column widths like this:
from tabulate import tabulate data = [ ["Ahmed Ali", 30, "Software Engineer"], ["Fatima Hassan", 28, "Data Scientist"], ["Omar Youssef", 35, "Project Manager"] ] headers = ["Name", "Age", "Profession"] print(tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[15]*3))
Output:
+---------------+-------+-----------------+ | Name | Age | Profession | +===============+=======+=================+ | Ahmed Ali | 30 | Software | | | | Engineer | +---------------+-------+-----------------+ | Fatima Hassan | 28 | Data Scientist | +---------------+-------+-----------------+ | Omar Youssef | 35 | Project Manager | +---------------+-------+-----------------+
This code sets a maximum width of 15 characters for each column. The content is truncated if it exceeds this limit.
Set individual widths for specific columns
To set different widths for each column, you can provide a list of widths to the maxcolwidths
parameter.
You can customize individual column widths like this:
from tabulate import tabulate data = [ ["Ahmed Ali", 30, "Software Engineer"], ["Fatima Hassan", 28, "Data Scientist"], ["Omar Youssef", 35, "Project Manager"] ] headers = ["Name", "Age", "Profession"] print(tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[20, 5, 25]))
Output:
+--------------------+-----+-------------------------+ | Name | Age | Profession | +====================+=====+=========================+ | Ahmed Ali | 30 | Software Engineer | +--------------------+-----+-------------------------+ | Fatima Hassan | 28 | Data Scientist | +--------------------+-----+-------------------------+ | Omar Youssef | 35 | Project Manager | +--------------------+-----+-------------------------+
This code sets different widths for each column: 20 for the name, 5 for the age, and 25 for the profession.
Implement custom column width functions
To create more dynamic column widths, you can define a custom function to calculate widths based on the content.
You can implement a function to calculate dynamic widths like this:
from tabulate import tabulate def calculate_widths(data, headers, padding=2): widths = [max(len(str(item)) for item in col) for col in zip(*data)] header_widths = [len(h) for h in headers] return [max(w, hw) + padding for w, hw in zip(widths, header_widths)] data = [ ["Ahmed Ali", 30, "Software Engineer"], ["Fatima Hassan", 28, "Data Scientist"], ["Omar Youssef", 35, "Project Manager"] ] headers = ["Name", "Age", "Profession"] custom_widths = calculate_widths(data, headers) print(tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=custom_widths))
Output:
+---------------+-----+--------------------+ | Name | Age | Profession | +===============+=====+====================+ | Ahmed Ali | 30 | Software Engineer | +---------------+-----+--------------------+ | Fatima Hassan | 28 | Data Scientist | +---------------+-----+--------------------+ | Omar Youssef | 35 | Project Manager | +---------------+-----+--------------------+
This function calculates the maximum width needed for each column based on the content and headers, adding a small padding for readability.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.