英文:
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'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()
)
</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='h'`, use `output: asis` and print everything - see code example below:
```{python}
#| 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()`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论