Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning individual columns in pandas to_latex

I'm using the pandas to_latex method to convert a dataframe into a latex tabular. I don't see an option to change the alignment fields of the resulting tabular. For example, I have a dataframe that looks like this:

In [46]: df
Out[46]: 
    Number of days  Tuples Distinct Tuples
162             29  700587           41300
163             20  497599           29302
164             15  365599           21382
165             10  256903           14916
166              5  127647            7441
167              2   54254            3117
168              1   26987            1288

and my output table looks like this:

In [50]: print df.to_latex(index=None)
\begin{tabular}{lll}
\toprule
Number of days &  Tuples & Distinct Tuples \\
\midrule
            29 &  700587 &           41300 \\
            20 &  497599 &           29302 \\
            15 &  365599 &           21382 \\
            10 &  256903 &           14916 \\
             5 &  127647 &            7441 \\
             2 &   54254 &            3117 \\
             1 &   26987 &            1288 \\
\bottomrule
\end{tabular}

What I want is for the {lll} alignment to change to {rrr}. In general, I might even want different alignments for different columns, or even use a vertical separator | in a {r|r|r} designator.

Is this currently supported ?

like image 637
Suresh Avatar asked Jun 15 '14 14:06

Suresh


People also ask

How do I realign columns in pandas?

Reorder Columns using Pandas . Another way to reorder columns is to use the Pandas . reindex() method. This allows you to pass in the columns= parameter to pass in the order of columns that you want to use.

How do you change specific columns in pandas?

Use the pandas DataFrame. rename() function to modify specific column names. Set the DataFrame columns attribute to your new list of column names.

How do I reorder one column in pandas?

You need to create a new list of your columns in the desired order, then use df = df[cols] to rearrange the columns in this new order.

How do I offset a column in pandas?

shift() If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis.


2 Answers

By now (pandas version 0.17.1), the to_latex method has the column_format parameter, so you could simply do

print df.to_latex(index=None, column_format='rrr')
like image 139
proggy Avatar answered Oct 06 '22 01:10

proggy


As you can see in code of pandas(this function is used for rendering latex_table): https://github.com/pydata/pandas/blob/master/pandas/core/format.py#L492 There's not supported yet. Only if your data is numpy's number it will be formatted to right. In python it's pretty easy to format your columns.

print df.to_latex(index=None).replace('lll','rrr')

or in more generic way using regex replace.

like image 36
404pio Avatar answered Oct 06 '22 01:10

404pio