pandas 如何在 IPython Notebook 中正确渲染数学表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21293678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:36:33  来源:igfitidea点击:

How to Render Math Table Properly in IPython Notebook

pandaslatexipythonipython-notebooksympy

提问by Titanic

The math problem that I'm solving gives different analytical solutions in different scenarios, and I would like to summarize the result in a nice table. IPython Notebook renders the list nicely: for example:

我正在解决的数学问题在不同的场景下给出了不同的解析解,我想用一个漂亮的表格来总结结果。IPython Notebook 可以很好地呈现列表:例如:

import sympy
from pandas import DataFrame
from sympy import *
init_printing()
a, b, c, d = symbols('a b c d')
t = [[a/b, b/a], [c/d, d/c]]
t

enter image description here

在此处输入图片说明

However, when I summarize the answers into a table using DataFrame, the math cannot be rendered any more:

但是,当我使用 DataFrame 将答案汇总到表格中时,数学无法再呈现:

df = DataFrame(t, index=['Situation 1', 'Situation 2'], columns=['Answer1','Answer2'])
df

enter image description here

在此处输入图片说明

"print df.to_latex()" also gives the same result. I also tried "print(latex(t))" but it gives this after compiling in LaTex, which is alright, but I still need to manually convert it to a table:

“print df.to_latex()”也给出了相同的结果。我也试过 "print(latex(t))" 但它在 LaTex 中编译后给出了这个,没关系,但我仍然需要手动将其转换为表格:

enter image description here

在此处输入图片说明

How should I use DataFrame properly in order to render the math properly? Or is there any other way to export the math result into a table in Latex? Thanks!

我应该如何正确使用 DataFrame 以正确呈现数学?或者有没有其他方法可以将数学结果导出到 Latex 中的表格中?谢谢!

Update: 01/25/14 Thanks again to @Jakob for solving the problem. It works perfectly for simple matrices, though there are still some minor problems for more complicated math expressions. But I guess like @asmeurer said, perfection requires an update in IPython and Pandas.

更新:01/25/14 再次感谢@Jakob 解决了这个问题。它适用于简单的矩阵,但对于更复杂的数学表达式仍然存在一些小问题。但我想就像@asmeurer 所说的,完美需要 IPython 和 Pandas 的更新。

enter image description here

在此处输入图片说明

Update: 01/26/14 If I render the result directly, i.e. just print the list, it works fine: enter image description here

更新:01/26/14 如果我直接渲染结果,即只打印列表,它工作正常: 在此处输入图片说明

采纳答案by Jakob

MathJax is currently not able to render tables, hence the most obvious approach (pure latex) does not work.

MathJax 目前无法渲染表格,因此最明显的方法(纯乳胶)不起作用。

However, following the advise of @asmeurer you should use an html table and render the cell content as latex. In your case this could be easily achieved by the following intermediate step:

但是,按照@asmeurer 的建议,您应该使用 html 表格并将单元格内容呈现为乳胶。在您的情况下,这可以通过以下中间步骤轻松实现:

from sympy import latex
tl = map(lambda tc: '$'+latex(tc)+'$',t)
df = DataFrame(tl, index=['Situation 1', 'Situation 2'], columns=['Answer'])
df

which gives:
enter image description here

这使:
在此处输入图片说明



Update:

更新:

In case of two dimensional data, the simple map function will not work directly. To cope with this situation the numpy shape, reshapeand ravelfunctions could be used like:

在二维数据的情况下,简单的地图功能将无法直接工作。为了应对这种情况,可以使用numpy shapereshaperavel函数,例如:

import numpy as np
t = [[a/b, b/a],[a*a,b*b]]
tl=np.reshape(map(lambda tc: '$'+latex(tc)+'$',np.ravel(t)),np.shape(t))
df = DataFrame(tl, index=['Situation 1', 'Situation 2'], columns=['Answer 1','Answer 2'])
df

This gives:
enter image description here

这给出:
在此处输入图片说明



Update 2:

更新 2:

Pandas crops cell content if the string length exceeds a certain number. E.g a more complicated expression like

如果字符串长度超过某个数字,Pandas 会裁剪单元格内容。例如更复杂的表达式,如

t1 = [a/2+b/2+c/2+d/2]
tl=np.reshape(map(lambda tc: '$'+latex(tc)+'$',np.ravel(t1)),np.shape(t1))
df = DataFrame(tl, index=['Situation 1'], columns=['Answer 1'])
df

gives:
enter image description here

给出:
在此处输入图片说明

To cope with this issue a pandas package option has to be altered, for details see here. For the present case the max_colwidthhas to be changed. The default value is 50, hence let's change it to 100:

为了解决这个问题,必须更改一个 pandas 包选项,有关详细信息,请参见此处。对于目前的情况max_colwidth,必须更改。默认值为 50,因此让我们将其更改为 100:

import pandas as pd
pd.options.display.max_colwidth=100
df

gives:
enter image description here

给出:
在此处输入图片说明