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 ?
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.
Use the pandas DataFrame. rename() function to modify specific column names. Set the DataFrame columns attribute to your new list of column names.
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.
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.
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')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With