[Tex/LaTex] Export Dataframe to Table in Latex

pythontables

enter image description here

I am new to Latex

I need to add the data of above into table (in latex). I have a pandas DataFrame which evaluates this result. and I know the use of .to_latex(). But after that I don't know how to add this data into table?

Best Answer

The to_latex() makes a finished tabular, so you just need to write that to a file, and use \input in your LaTeX document.

Assuming Python 3, here is a minimal Python example to make a DataFrame and save as a text file:

import pandas as pd
df = pd.DataFrame(['Foo','Bar','Baz'])
with open('mytable.tex','w') as tf:
    tf.write(df.to_latex())

A minimal LaTeX example using the exported table code, will then be

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\input{mytable}
\end{document}

The booktabs package is required as to_latex() uses its commands for table rules. This assumes that mytable.tex is in the same folder.