引数は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}
私はこれがうまくいくと思いますが、私が表示している列の数に基づいて文字列を動的に生成しなければならないと思います。本当に他に何もないなら、私はそれを取るでしょう。 – FooBar
しかし、それほど悪くはありません: 'column_format = 'r' * len(df.columns)'。 –
@FooBarこれまたは任意の回答があなたの質問を解決した場合、チェックマークをクリックして[受諾](http://meta.stackexchange.com/q/5234/179419)を検討してください。これは、あなたが解決策を見つけ出し、回答者とあなた自身の両方に評判を与えていることを広範なコミュニティに示します。これを行う義務はありません。 –