2016-07-12 6 views
0

を正当化:to_latex()(TO_STRING機能はありません)の<code>to_string()</code>からパラメータ

正当化:{ 『左』、 『』右}、デフォルトなし 左または列のラベルを右詰め。 Noneが>印刷設定(set_optionによって制御される)からオプションを使用する場合は、そのままの状態で '右'にします。

To_latex()このようなパラメータはありません。有効な代替手段は何ですか?

答えて

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} 
+0

私はこれがうまくいくと思いますが、私が表示している列の数に基づいて文字列を動的に生成しなければならないと思います。本当に他に何もないなら、私はそれを取るでしょう。 – FooBar

+0

しかし、それほど悪くはありません: 'column_format = 'r' * len(df.columns)'。 –

+0

@FooBarこれまたは任意の回答があなたの質問を解決した場合、チェックマークをクリックして[受諾](http://meta.stackexchange.com/q/5234/179419)を検討してください。これは、あなたが解決策を見つけ出し、回答者とあなた自身の両方に評判を与えていることを広範なコミュニティに示します。これを行う義務はありません。 –

関連する問題