From the course: Data Science Reporting with Quarto for Python
Displaying tables of data in Quarto
From the course: Data Science Reporting with Quarto for Python
Displaying tables of data in Quarto
- [Instructor] We are in the 04_02b folder, and we're going to look at how to format tables in your Quarto document. First of all, let's look at HTML. So let's render this html_tables.qmd file with Command+Shift+K. We've got several different types of tables in here, Markdown, static tables of data, and interactive tables as well. So, Markdown tables allow you to write data as a table in Markdown. It's really convenient if it's a quick summary table you want to add to your document without having to create a data file, which you then import using Python, and display. But you're probably going to want to display tables of actual data. So here, we can see what happens if we return a DataFrame. If we return a DataFrame, we get a fairly good-looking table. But the real value of HTML is we can create interactive tables. And the best choice available to you is itables. So here we have the default options for itables. So it gives us an interactive table, so we can sort, we can search. So we could search for 210 if we wanted to in the dataset. Those for defaults. How about customizing the table? Well, if we scroll down a little bit, we can see that I've written a little bit of custom JavaScript to highlight the NA values in the columns, specifically columns 2, 3, 4, and 5. So if we scroll down, we can see on row 4 of the data, those NaN values are red. To learn more about what's possible with itables, please visit the itables website, where you'll discover that it builds on the DataTables JavaScript framework, which is why I wrote custom JavaScript to color those NA values. Now let's take a look at PDF. So we'll open the pdf_tables.qmd file. Let's move it over to the left just for some consistency. And let's render this document with Command+Shift+K. Now that's finished rendering, we can see that our Markdown table is working much the same way as before, it just looks a little bit different because this is a PDF document. Then if we look at our tables of data, we can see that we've just returned penguins_cleaned DataFrame, and it looks like this. It spills out over the page and it doesn't look that great. Currently, the best option for customizing how your tables of data look in PDF is using the to_latex function from PDF and providing CSS. So you can see what I've done here is I've applied font weight bold to the column labels. And there's more that you can do if you look at the documentation for to_latex. Finally, let's look at Word tables. So I'm going to open up word_tables.qmd. There is on the left, and we're going to render this document with Command+Shift+K. When this finishes rendering, we're not going to see a preview of the document inside of Visual Studio because it doesn't know how to display Word documents. What we're going to do is we're going to download our DOCX file. So let's right-click on the file, click on Download, and let's download that to our Downloads folder. I already have the file here, so we'll replace it. And let's go over to our Downloads folder. And we'll open that file up. And here we can see nicely formatted tables. These don't look like the default tables in Word, why is that? Well, let's go to Visual Studio. Back to our QMD file. And you can see that we are making use of a reference doc. Reference docs are how you can customize the styles in a Word document created with Quarto, which is covered in a future chapter. Now let's go back to our document. And so we can see our Markdown tables created with Markdown. And when we go for tables of data, we can see the default options for a DataFrame. So it looks okay, it doesn't include the index of the row, which isn't that helpful. So if we scroll down in the document, we can see that I make use of the tabulate package, which is currently the best option for custom formatting Word tables from Python objects. So you can see what I'm doing here is I'm replacing the underscores and the column headings with a dot, and I'm also setting the option showindex, never, so I get a much nicer-looking table. And that's everything that you need to know about how to insert tables of data and Markdown tables into your Quarto documents.
Contents
-
-
-
-
-
-
(Locked)
Using Matplotlib and Plotnine in Quarto4m 16s
-
Displaying tables of data in Quarto5m 9s
-
(Locked)
Displaying images in Quarto4m 25s
-
(Locked)
Inserting calculated values into sentences3m 34s
-
(Locked)
Adding captions and cross-references to Quarto8m 35s
-
(Locked)
Challenge: Create a rich Quarto report30s
-
(Locked)
Solution: Create a rich Quarto report8m 39s
-
(Locked)
-
-
-
-