Quarto: 放置 Python 表格

huangapple go评论78阅读模式
英文:

Quarto: Placement of python tables

问题

我正在使用.qmd文档来撰写我的论文,主要使用Python。不时,我想展示数据的预览 - 在大多数情况下通过df.head()。我的问题是,当使用df.head().style时,数据框会浮动在PDF中(似乎需要使用style来在Quarto内部将表格转换为kbl()以使其看起来漂亮)。

在Quarto文档中,我没有找到可以设置以避免浮动表格的任何标志。我注意到#| fig-pos = 'h'有一个标志,但没有找到tbl-pos的等效项。

我希望将pandas数据框作为表格写入,而不浮动。

更新

这里是您方便复制的可重现代码:

#| label: tbl-ohe_example
#| tbl-cap: '独热向量化示例'
#| tbl-column: body

(
    pd.DataFrame(
        data = {
            '句子': ['我喜欢吃苹果', '我喜欢吃香蕉'],
            '标记' : [['我', '喜欢', '吃', '苹果'], ['我', '喜欢', '吃', '香蕉']],
            '向量' : [[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1]],
        }
    )
    .style # 需要将df.head()转换为kbl格式
    .hide_index() 
)

<details>
<summary>英文:</summary>

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&#39;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 = &#39;h&#39;`, 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: &#39;One-hot vectorization example&#39;
#| tbl-column: body

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

</details>


# 答案1
**得分**: 0

以下是翻译好的内容:

要解决这个问题的方法是:使用pandas的`.to_latex()`,并添加参数`position='h'`,使用`output: asis`并打印所有内容 - 请参见下面的代码示例:

```{python}
#| 标签: tbl-ohe_example
#| 表标题: '独热向量化示例'
#| 表列: body
#| 输出: asis

print(
    pd.DataFrame(
        data = {
            '句子': ['我喜欢吃苹果', '我喜欢吃香蕉'],
            '标记' : [['我', '喜欢', '吃', '苹果'], ['我', '喜欢', '吃', '香蕉']],
            '向量' : [[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1]],
        }
    )
    .to_latex(
        index = False,
        position = 'h',
        ... # 在此处添加其他参数
    )
)
```

我个人甚至使用了`tabularx`而不是`tabular`,这可以通过直接使用`str.replace()`来实现。

<details>
<summary>英文:</summary>

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

```{python}
#| label: tbl-ohe_example
#| tbl-cap: &#39;One-hot vectorization example&#39;
#| tbl-column: body
#| output: asis

print(
    pd.DataFrame(
        data = {
            &#39;Sentence&#39;: [&#39;I like to eat apples&#39;, &#39;I like to eat bananas&#39;],
            &#39;Tokens&#39; : [[&#39;I&#39;, &#39;like&#39;, &#39;to&#39;, &#39;eat&#39;, &#39;apples&#39;], [&#39;I&#39;, &#39;like&#39;, &#39;to&#39;, &#39;eat&#39;, &#39;bananas&#39;]],
            &#39;Vector&#39; : [[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1]],
        }
    )
    .to_latex(
        index = False,
        position = &#39;h&#39;,
        ... # place other arguments here
    )
)
```

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

</details>



huangapple
  • 本文由 发表于 2023年3月8日 18:00:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定