简体   繁体   English

乳胶的熊猫样式选项

[英]pandas style options to latex

Pandas has two nice functionalities I use a lot - that's the df.style... option and the df.to_latex() call. Pandas 有两个很好的功能,我经常使用——那就是df.style...选项和df.to_latex()调用。 But I do not know how to combine both.但我不知道如何将两者结合起来。

The .style option makes looking at tables much more pleasant. .style 选项使查看表格更加愉快。 It lets you grasp information rapidly because of visual enhancements.由于视觉增强,它可以让您快速掌握信息。 This works perfectly in a jupyter notebook, for example.例如,这在 jupyter notebook 中非常有效。 Here is an arbitrary example I copied from the documentation .这是我从文档中复制的任意示例。

df.style.bar(subset=['A', 'B'], align='mid', color=['#d65f5f', '#5fba7d'])

This yields:这产生:

带有样式列的数据框

However, as nice as this looks in a jupyter notebook, I can not put this to latex code.但是,尽管这在 jupyter 笔记本中看起来不错,但我无法将其放入 Latex 代码中。 I get the following error message instead, if chaining a 'to_latex()' call at the end of my visual enhancements: AttributeError: 'Styler' object has no attribute .如果在我的视觉增强结束时链接一个 'to_latex()' 调用,我会收到以下错误消息: AttributeError: 'Styler' object has no attribute Does that mean it's simply not possible, because the displayed colorful table is not a DataFrame object any more, but now a Styler object, now?这是否意味着它根本不可能,因为显示的彩色表格不再是 DataFrame 对象,而是现在的 Styler 对象?

Is there any workaround?有什么解决方法吗? At least with easier tables, let's say where only cells have a single background color with respect to their value, instead of a 'complicated' bar graph.至少对于更简单的表格,假设只有单元格具有与其值相关的单一背景颜色,而不是“复杂”的条形图。

Instead of trying to export this formatting to bulky LaTeX markup, I would go the route explored already over in TeX.SE: add the functionality as LaTeX code that draws similar formatting based on the same data.我不会尝试将这种格式导出为庞大的 LaTeX 标记,而是走在 TeX.SE 中已经探索过的路线:将功能添加为基于相同数据绘制类似格式的 LaTeX 代码。

I don't know how you can use latex directly but you can use df.to_html().我不知道如何直接使用乳胶,但可以使用 df.to_html()。 Once you get the html you can style it as you like and then use any of the following.获得 html 后,您可以根据需要设置样式,然后使用以下任何一种。

  1. Python's html2latex Python 的 html2latex
  2. Website :HTML to TEX Converter网站 :HTML to TEX Converter
  3. Python's weasyprint Python 的 weasyprint
  4. Pandoc潘多克

Here is one latex package that I found on googling that talks about embedding html in latex.这是我在谷歌上找到的一个乳胶包,它讨论了在乳胶中嵌入 html。 https://alvinalexander.com/blog/post/latex/use-html-package-in-latex-control-your-output/ https://alvinalexander.com/blog/post/latex/use-html-package-in-latex-control-your-output/

I once only used latex and embedded images of tables when I used it.我曾经在使用它时只使用乳胶和嵌入的表格图像。

从 pandas v1.3.0 开始,这些现在合并在pandas.io.formats.style.Styler.to_latex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM