By the end of this lesson, you will be able to:
:information_source: What is PrettyTable? PrettyTable is a simple Python package that helps you display data in beautiful, easy-to-read tables. Instead of showing messy data, PrettyTable organizes your information into neat rows and columns!
When you're working with data, you want to make it easy for people to understand. PrettyTable helps you:
:bulb: Learn More Want to explore more features? Check out the official documentation: https://pypi.org/project/prettytable/
You can practice with real data from the Malaysia Open Data Portal: https://www.data.gov.my/. This website has lots of interesting datasets you can use in your projects!
Here are the basic steps you'll follow when using PrettyTable:
Before you can use PrettyTable, you need to install it!
Open your terminal and type:
py -m pip install prettytable
:memo: Google Colab Users Good news! If you're using Google Colab, PrettyTable is already installed. You can skip the installation step!
After installing, you need to import PrettyTable into your Python program:
from prettytable import PrettyTable
This line tells Python you want to use PrettyTable in your code.
Let's create your first table! You'll use PrettyTable()
to make a new empty table. Remember to save it in a variable so you can use it later:
table1 = PrettyTable()
Now you have an empty table called table1
ready to fill with data!
To see your table, use the print()
function. Let's see what an empty table looks like:
print(table1)
Expected output:
++
| |
++
++
That's your empty table! It's waiting for you to add some data.
Made a mistake? No problem! You can clear all the data from your table and start fresh using the clear()
method:
table1.clear()
This removes all data but keeps the table ready for new information.
Now for the fun part - adding data! There are two ways to do this:
Let's practice with real data: "Statistics of not following SOP in Malaysia 2020" from the Malaysia Open Data Portal.
Field names are the headers at the top of your table. They tell people what each column contains:
table1.field_names = ["Fail to", "Aug", "Sep", "Oct", "Nov"]
These headers describe what's in each column.
Expected output:
+----------+-----+-----+-----+-----+
| Fail to | Aug | Sep | Oct | Nov |
+----------+-----+-----+-----+-----+
+----------+-----+-----+-----+-----+
Great! Your table now has headers but no data yet.
Now let's add the actual data, one row at a time using add_row()
:
table1.add_row(["Social Distancing", 1973, 1530, 3787, 3424])
table1.add_row(["Wear Mask", 413, 617, 5051, 3155])
Each row contains data for one category across all months.
Expected output:
+-----------------+------+------+------+------+
| Fail to | Aug | Sep | Oct | Nov |
+-----------------+------+------+------+------+
|Social Distancing| 1973 | 1530 | 3787 | 3424 |
| Wear Mask | 413 | 617 | 5051 | 3155 |
+-----------------+------+------+------+------+
Great! Your table now has headers but no data yet.
Sometimes it's easier to add data column by column. Use add_column(field_name, data)
for this:
table1.add_column("Fail to",["Social Distancing", "Wear Mask"])
table1.add_column("Aug",[1973, 413])
table1.add_column("Sep",[1530, 617])
table1.add_column("Oct",[3787, 5051])
table1.add_column("Nov",[3424, 3155])
Each column contains all the data for that field.
:memo: Same Result! Both methods (adding rows or adding columns) create the exact same table. Choose the method that works best for your data!
Make your table even clearer by adding a title! Use get_string(title="Your Title")
when printing:
print(table1.get_string(title="Statistics of not following SOP in Malaysia 2020"))
The title appears at the top of your table.
Expected output:
+-------------------------------------------------+
|Statistics of not following SOP in Malaysia 2020 |
+---------------------+------+------+------+------+
| Fail to | Aug | Sep | Oct | Nov |
+---------------------+------+------+------+------+
| Social Distancing | 1973 | 1530 | 3787 | 3424 |
| Wear Mask | 413 | 617 | 5051 | 3155 |
+---------------------+------+------+------+------+
Your table now has a professional title that explains what the data shows!
You've learned how to:
PrettyTable makes your data look professional and easy to read. Practice with different types of data to become a table-making expert!
Code with AI: Try using AI to work with PrettyTable for creating formatted tables.
Prompts to Try:
Create your own table showing:
Remember: The more you practice, the better you'll get at creating beautiful tables!