2

I want to make a Pandas DataFrame in which some columns are of Latex formula (IPython.core.display.Latex) type. When I display the DataFrame in Jupyter Notebook, the formulae are not displayed, instead I see only the type name. Is there any way to show the formulae when they are some elements of a Pandas DF?

import pandas as pd
from IPython.display import Latex

my_dict = {'Case1':{'Formula1':Latex('$$x^{-1}$$'), 'Formula2':Latex('$$x^2$$')},
           'Case2':{'Formula1':Latex('$$x^{-2}$$'), 'Formula2':Latex('$$x^4$$')}}

df = pd.DataFrame(my_dict)
display(df.transpose())
sophros
  • 14,672
  • 11
  • 46
  • 75
Esi
  • 467
  • 3
  • 12
  • Does this answer your question? [How to write LaTeX in IPython Notebook?](https://stackoverflow.com/questions/13208286/how-to-write-latex-in-ipython-notebook) – G. Anderson Nov 06 '20 at 21:59
  • Please consider marking the answer as accepted (grey tick mark on the left of the answer) and upvoting it if you find it useful. – sophros Feb 15 '21 at 10:25

1 Answers1

2

Interestingly, if you output only one field of the dataframe it works:

df['Case1']['Formula1']

−1

However, when the whole dataframe is taken into account you cannot use Latex objects. It is enough to use only LaTeX formulas between $$ characters:

my_dict = {'Case1':{'Formula1':'$$x^{-1}$$', 'Formula2':'$$x^2$$'},
           'Case2':{'Formula1':'$$x^{-2}$$', 'Formula2':'$$x^4$$'}}
df = pd.DataFrame(my_dict)
display(df.transpose())

enter image description here

sophros
  • 14,672
  • 11
  • 46
  • 75