Open the file in Positron/VS Code. Replace the title, name, text, and code. Leave eval: false in the graph and table cells until you add your data; then change it to true.
---title: "Document title"author: "Your name"format: html: embed-resources: true pdf: defaultjupyter: python3---# HeadingYour text here. Use **bold** or *italics*.## SubheadingYour text here.```{python}import pandas as pdimport matplotlib.pyplot as pltfrom IPython.display import Markdown, display# Add your Python code here.```## Graph```{python}#| eval: false#| label: fig-graph#| fig-cap: "Figure caption"# Define x and y in the cell above, then change eval to true.plt.plot(x, y)plt.xlabel("X-axis label")plt.ylabel("Y-axis label")plt.show()```## Table```{python}#| eval: false#| label: tbl-table#| tbl-cap: "Table caption"# Create a DataFrame called df above, then change eval to true.display(Markdown(df.to_markdown(index=False, floatfmt=".2f")))```
Quarto can calculate results in Python and turn them into a formatted table whenever you render the document. You do not need to copy numbers into a table by hand.
Copy the following cell into your .qmd file. It uses small, invented data so you can run it immediately; replace the example data with your own when you are ready.
import pandas as pdfrom IPython.display import Markdown, display# Example data; replace with your own data.df = pd.DataFrame({"Income": [25000, 32000, 41000, 28000, 35000],"Age": [24, 35, 48, 29, 41]})summary = ( df.describe() .loc[["count", "mean", "std", "min", "max"]] .rename_axis("Statistic") .reset_index())display(Markdown(summary.to_markdown(index=False, floatfmt=".2f")))
Table 1: Summary statistics
Statistic
Income
Age
count
5.00
5.00
mean
32200.00
35.40
std
6220.93
9.50
min
25000.00
24.00
max
41000.00
48.00
The table above is calculated from the example data during rendering. count is the number of non-missing observations, mean is the average, std is the sample standard deviation, and min and max are the smallest and largest values.
Captions, references, and formatting
label: tbl-summary gives the table a unique name for cross-references. Table labels must start with tbl-.
tbl-cap supplies its caption. Quarto numbers the table automatically.
index=False leaves out the DataFrame’s row numbers; floatfmt=".2f" displays floating-point values to two decimal places.
Add #| echo: false at the top of the cell to hide the Python code in the finished document while keeping the table visible. The code is shown here so you can copy it.
To refer to the table in your writing, type:
Summary statistics appear in @tbl-summary.
Quarto turns that into: Summary statistics appear in Table 1.
Use your own results
Replace the example data with your own DataFrame, or build a DataFrame from the results of your calculations. To display those results directly instead of calculating summary statistics, use:
The downloadable template already includes this line in its table cell. Define df first, then change that cell’s #| eval: false to #| eval: true. The complete example above includes its own data and does not need an eval: false option.
Render again after changing the data or calculations to update the table. This Markdown-table approach works in HTML and PDF and uses the tabulate package from the installation instructions.
---title: "Document template"---[Download document-template.qmd](../downloads/document-template.qmd){.btn .btn-primary}Open the file in Positron/VS Code. Replace the title, name, text, and code. Leave `eval: false` in the graph and table cells until you add your data; then change it to `true`.Use [Workflow](../workflow.qmd) for previewing and rendering.````markdown---title: "Document title"author: "Your name"format: html: embed-resources: true pdf: defaultjupyter: python3---# HeadingYour text here. Use **bold** or *italics*.## SubheadingYour text here.```{python}import pandas as pdimport matplotlib.pyplot as pltfrom IPython.display import Markdown, display# Add your Python code here.```## Graph```{python}#| eval: false#| label: fig-graph#| fig-cap: "Figure caption"# Define x and y in the cell above, then change eval to true.plt.plot(x, y)plt.xlabel("X-axis label")plt.ylabel("Y-axis label")plt.show()```## Table```{python}#| eval: false#| label: tbl-table#| tbl-cap: "Table caption"# Create a DataFrame called df above, then change eval to true.display(Markdown(df.to_markdown(index=False, floatfmt=".2f")))```````[Code-cell options](https://quarto.org/docs/computations/execution-options.html) · [Tables](https://quarto.org/docs/authoring/tables.html)# Create tables automatically {#automatic-tables}Quarto can calculate results in Python and turn them into a formatted table whenever you render the document. You do not need to copy numbers into a table by hand.Copy the following cell into your `.qmd` file. It uses small, invented data so you can run it immediately; replace the example data with your own when you are ready.```{python}#| label: tbl-summary#| tbl-cap: "Summary statistics"import pandas as pdfrom IPython.display import Markdown, display# Example data; replace with your own data.df = pd.DataFrame({"Income": [25000, 32000, 41000, 28000, 35000],"Age": [24, 35, 48, 29, 41]})summary = ( df.describe() .loc[["count", "mean", "std", "min", "max"]] .rename_axis("Statistic") .reset_index())display(Markdown(summary.to_markdown(index=False, floatfmt=".2f")))```The table above is calculated from the example data during rendering. `count` is the number of non-missing observations, `mean` is the average, `std` is the sample standard deviation, and `min` and `max` are the smallest and largest values.## Captions, references, and formatting- `label: tbl-summary` gives the table a unique name for cross-references. Table labels must start with `tbl-`.- `tbl-cap` supplies its caption. Quarto numbers the table automatically.- `index=False` leaves out the DataFrame's row numbers; `floatfmt=".2f"` displays floating-point values to two decimal places.- Add `#| echo: false` at the top of the cell to hide the Python code in the finished document while keeping the table visible. The code is shown here so you can copy it.To refer to the table in your writing, type:```markdownSummary statistics appear in @tbl-summary.```Quarto turns that into: Summary statistics appear in @tbl-summary.## Use your own resultsReplace the example data with your own DataFrame, or build a DataFrame from the results of your calculations. To display those results directly instead of calculating summary statistics, use:```pythondisplay(Markdown(df.to_markdown(index=False, floatfmt=".2f")))```The downloadable template already includes this line in its table cell. Define `df` first, then change that cell's `#| eval: false` to `#| eval: true`. The complete example above includes its own data and does not need an `eval: false` option.Render again after changing the data or calculations to update the table. This Markdown-table approach works in HTML and PDF and uses the `tabulate` package from the [installation instructions](../installation.qmd#install-the-python-packages).[Python-generated tables in Quarto](https://quarto.org/docs/authoring/tables.html) · [pandas table conversion](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_markdown.html)