Please considering add a parameter to allow users to add \hline to the latex output so that each two rows will have a horizontal line between them.

For example:

import pandas as pd

df = pd.DataFrame(data={'col_1': [1, 2, 4],
                        'col_2': [4, 3, 2]})
print(df.to_latex(index=False))

It will give me:

\begin{tabular}{rr}
\toprule
 col\_1 &  col\_2 \\
\midrule
     1 &      4 \\
     2 &      3 \\
     4 &      2 \\
\bottomrule
\end{tabular}

I wish the output could be

\begin{tabular}{rr}
\toprule
\hline
 col\_1 &  col\_2 \\
\hline
\midrule
     1 &      4 \\ \hline
     2 &      3 \\ \hline
     4 &      2 \\ \hline
\bottomrule
\end{tabular}

Comment From: ghost

As a temporal solution, you can use "replace" function for strings in Python, to change those " \\ " at the end of your table rows by "\\ \hline" to have the horizontal lines.

You need before to save your lateX code in a variable as below :

latex_code = df.to_latex(index=False)

and then you perform the replace :

latex_code = latex_code.replace("\\\n", "\\ \hline\n") print(latex_code)

Best.

Comment From: Ademord

@MoMoussabbih how does the merge help with this? i cant seem to undestand how to do it today without the replace you recommend.

Comment From: riklopfer

@jreback how can we use the code from that pr to solve this issue? I don't see it either.

Comment From: DanielHabenicht

@attack68 maybe you can shed light on how one can add \hline to each row for latex?

I guess it could be done by using something along the lines of (Not working!):

.set_table_styles([
        {'selector': 'toprule', 'props': ':toprule;'},
        {'selector': 'bottomrule', 'props': ':hline;'},
        {'selector': 'td', 'props': ':hline;'},
    ], overwrite=False)

But I can't seem to get the right selector. Or is it simply not possible? (how is it done for vertical lines? should I create an extra issue?)

Comment From: attack68

No that doesnt work. The selctors are very specific, as documented. You cannot add HTML selectors and expect it to work - its not coded. (also td is a cell not a row, tr is a row and that wouldnt work either)

If clines does not work for you as required, you could maybe try to be clever and use the format_index to add an hline at the beginning of each row. This is obviously not the intended or expected route but it does work:

This will a bit chaotic with adding styles and css conversion though.

Pandas [FR] add option to format \hline in to_latex()

Comment From: DanielHabenicht

Thanks, I think I got it now. For a fully lined table one would execute:

df = pd.DataFrame([["a"], ["b"]])
print(df.style.set_table_styles([
    {'selector': 'toprule', 'props': ':hline;'},
    {'selector': 'midrule', 'props': ':hline;'},
    {'selector': 'bottomrule', 'props': ':hline;'},
], overwrite=False).to_latex(clines="all;data",  column_format="|l|l|"))

Resulting in:

\begin{tabular}{|l|l|}
\hline
 & 0 \\
\hline
0 & a \\
\cline{1-2}
1 & b \\
\cline{1-2}
\hline
\end{tabular}