Search code examples
pythonpandaspdfquarto

Quarto: Placement of python tables


I am using a .qmd document to write my thesis, which is mainly in Python. Now and then, I would like to present a preview of the data - in the most cases by df.head(). My problem is, that when using df.head().style, the data frame is floating somewhere in the pdf (style seems to be needed to quarto internally convert the table to a kbl() - in order to look nice).

In the quarto documentation I wasn't able to find any flags which can be set in order to avoid floating tables. I noticed that there is a flag for #| fig-pos = 'h', but the equivalent for tbl-pos was not found.

I wish to write a pandas Dataframe as table without floating.

Update

here the reproducible code for your convenience:

```{python}
#| label: tbl-ohe_example
#| tbl-cap: 'One-hot vectorization example'
#| tbl-column: body

(
    pd.DataFrame(
        data = {
            'Sentence': ['I like to eat apples', 'I like to eat bananas'],
            'Tokens' : [['I', 'like', 'to', 'eat', 'apples'], ['I', 'like', 'to', 'eat', 'bananas']],
            'Vector' : [[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1]],
        }
    )
    .style # Needed to convert df.head() to kbl format
    .hide_index() 
)
```

Solution

  • The way to solve this issue is: Use .to_latex() from pandas, and add argument position='h', use output: asis and print everything - see code example below:

    #| label: tbl-ohe_example
    #| tbl-cap: 'One-hot vectorization example'
    #| tbl-column: body
    #| output: asis
    
    print(
        pd.DataFrame(
            data = {
                'Sentence': ['I like to eat apples', 'I like to eat bananas'],
                'Tokens' : [['I', 'like', 'to', 'eat', 'apples'], ['I', 'like', 'to', 'eat', 'bananas']],
                'Vector' : [[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1]],
            }
        )
        .to_latex(
            index = False,
            position = 'h',
            ... # place other arguments here
        )
    )
    

    I personally even used tabularx instead of tabular, which is feasible changing direct the string using str.replace().