来自to_string()
:
对齐:{'左','右'},默认无 向左或向右对齐列标签。如果None使用>中的选项打印配置(由set_option控制),“正确”开箱即用。
To_latex()没有这样的参数。什么是有效的替代方案?
答案 0 :(得分:2)
使用column_format
参数。
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}