From to_string()
:
justify : {'left', 'right'}, default None Left or right-justify the column labels. If None uses the option from the > print configuration (controlled by set_option), 'right' out of the box.
To_latex() doesnt feature such a parameter. What's a valid alternative?
Use the column_format
argument.
import io
import pandas as pd
raw_df = io.StringIO("""\
id1 id2 weights
0 a 2a 144.0
1 a 2b 52.5
2 b 2a 2.0
3 b 2e 1.0
""")
df = pd.read_csv(raw_df, delim_whitespace=True)
print(df.to_latex())
# Output:
#
# \begin{tabular}{lllr}
# \toprule
# {} & id1 & id2 & weights \\
# \midrule
# 0 & a & 2a & 144.0 \\
# 1 & a & 2b & 52.5 \\
# 2 & b & 2a & 2.0 \\
# 3 & b & 2e & 1.0 \\
# \bottomrule
# \end{tabular}
print(df.to_latex(column_format='rrrr'))
# Output:
#
# \begin{tabular}{rrrr}
# \toprule
# {} & id1 & id2 & weights \\
# \midrule
# 0 & a & 2a & 144.0 \\
# 1 & a & 2b & 52.5 \\
# 2 & b & 2a & 2.0 \\
# 3 & b & 2e & 1.0 \\
# \bottomrule
# \end{tabular}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.