0

for my thesis, I need a large dataframe as a LaTeX table. It contains 126 rows and 5 columns. I know, that there is a function df.to_latex(buf='citations.tex', largetable=True). However, when i run the function, it cuts of my strings in the columns! I am using jupyter notebook. So when i print my dataframe there, the strings in the columns get abbreviated. This is all right. But when I use the to_latex() function on my dataframe, the columns get abbreviated as well. Why is this happening?

My Dataframe:

title                                              authors                            journal                                     year  doi
A visualization and modeling tool for security...  Reijo M. Savola; Petri Heinonen    2011 Information Security for South Africa  2011  10.1109/ISSA.2011.6027518
Information security requirements – Interpreti...  Mariana Gerber; Rossouw von Solms  Computers & Security                        2008  https://doi.org/10.1016/j.cose.2008.07.009

After using df.head(2).to_latex() The LaTeX output:

'\\begin{tabular}{llllrl}\n\\toprule\n{} &                                              title &                            authors &                                     journal &  year &                                         doi \\\\\n\\midrule\n0 &  A visualization and modeling tool for security... &    Reijo M. Savola; Petri Heinonen &  2011 Information Security for South Africa &  2011 &                   10.1109/ISSA.2011.6027518 \\\\\n1 &  Information security requirements – Interpreti... &  Mariana Gerber; Rossouw von Solms &                        Computers \\& Security &  2008 &  https://doi.org/10.1016/j.cose.2008.07.009 \\\\\n\\bottomrule\n\\end{tabular}\n'

As you can see, the textoutput is not an output of the dataframe, but rather from the printed version. Even exporting to a file, doesn't help. df.to_latex('citations.tex', longtable=True) is the command used, but it doesn't work as expected.

Why is this happening, and how to fix it?

Martin Müsli
  • 1,031
  • 3
  • 14
  • 26

2 Answers2

2

I hope you managed to solve this by now, but just in case...

I had the same problem and stumbled upon your post here. As the previous solution only seems to apply for printing dataframes in the console, but not in the to_latex function, it didn't work for me.

I found this issue here: https://github.com/pandas-dev/pandas/issues/6491 and could solve the problem for me using

pd.set_option('max_colwidth', 1000)

or the solution proposed there:

with pd.option_context("max_colwidth", 1000):
  print df.to_latex()
Harmageddon
  • 61
  • 1
  • 4
0

Do you see a difference on your output if you set this:

 import pandas as pd
 pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
marian.vladoi
  • 7,663
  • 1
  • 15
  • 29
  • Nope, it stays the same. Columns get still abreviated, no matter if I use `df.head()`, `df.head().to_latex` or `df` – Martin Müsli Nov 07 '19 at 12:45
  • you can check this [link](https://stackoverflow.com/questions/11707586/how-do-i-expand-the-output-display-to-see-more-columns) – marian.vladoi Nov 07 '19 at 12:48