Convert Pandas Data Frame To Latex File Code Example

Snippet 1

  # import DataFrame 
import pandas as pd 
  
# using DataFrame.to_latex() method 
gfg = pd.DataFrame({'Name': ['Marks', 'Gender'], 
                    'Jitender': ['78', 'Male'],  
                    'Purnima': ['78.9', 'Female']}) 
  
print(gfg.to_latex(index = False, multirow = True)) 
 

Snippet 2

  with open('mytable.tex', 'w') as tf:
     tf.write(df.to_latex()) 

Snippet 3

  import pandas as pd
df = pd.DataFrame({"a":range(10), "b":range(10,20)})
with open("my_table.tex", "w") as f:
    f.write("\begin{tabular}{" + " | ".join(["c"] * len(df.columns)) + "}n")
    for i, row in df.iterrows():
        f.write(" & ".join([str(x) for x in row.values]) + " \\n")
    f.write("\end{tabular}") 

Copyright © Code Fetcher 2020

 

 

Leave a comment

Your email address will not be published. Required fields are marked *