7

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?

1 Answer 1

15

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.

12
  • I tried the above thing . I tried to print content that work fine. but I am unable to get my table in my latex pdf :( Oct 1, 2017 at 19:05
  • @user144631 What exactly did you do, and exactly how did it fail? (Did you print to a file? Did you \input that file in your document?) Oct 1, 2017 at 19:09
  • I got now . Thankyou for your help :D Oct 1, 2017 at 19:12
  • No worries. Consider upvoting and/or accepting the answer. (The same goes for the answers to your other question.) Oct 1, 2017 at 19:16
  • Sure , Bdw is there any method to drop index of dataframe before writing to latex table ? Oct 1, 2017 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .