Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex formula in Pandas DataFrame

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())
like image 522
Esi Avatar asked Feb 02 '26 10:02

Esi


1 Answers

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

like image 69
sophros Avatar answered Feb 03 '26 23:02

sophros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!