2015-08-15 74 views
3

我想將一些DataFrame導出到LaTeX,但這些DataFrame有很多列,但沒有那麼多項目。解決方案是顯示轉置表。我知道熊貓'transpose,但我想格式化我的行,方法to_latex只支持按列格式化。將pandas DataFrame導出到LaTeX並按行應用格式化器

一些代碼:

import pandas as pd 

names = ['Bob','Jessica','Mary','John','Mel'] 
births = [968, 155, 77, 578, 973] 
tralala = [0.1, 0.2, 0.3, 0.4, 0.5] 
BabyDataSet = zip(names,births,tralala) 
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births', 'Whatever']) 
df = df.transpose() 
print df.to_latex() 

輸出:

\begin{tabular}{llllll} 
\toprule 
{} & 0 &  1 &  2 &  3 & 4 \\ 
\midrule 
Names & Bob & Jessica & Mary & John & Mel \\ 
Births & 968 &  155 & 77 & 578 & 973 \\ 
Whatever & 0.1 &  0.2 & 0.3 & 0.4 & 0.5 \\ 
\bottomrule 
\end{tabular} 

但是如果我想這個例如:

\begin{tabular}{llllll} 
\toprule 
{} & 0 &  1 &  2 &  3 & 4 \\ 
\midrule 
Names & Bob & Jessica & Mary & John & Mel \\ 
Births & 9.7e2 & 1.6e2 & 7.7e1 & 5.8e2 & 9.7e2 \\ 
Whatever & 0.1 &  0.2 & 0.3 & 0.4 & 0.5 \\ 
\bottomrule 
\end{tabular} 

有沒有辦法破解這個功能?我唯一想到的事情是手動應用格式轉換我的所有列字符串調換和出口

回答

0

之前,我想出了這一點:

for key in df.columns.values: 
    if key in formatters: 
     df[key] = df[key].apply(formatters[key]) 
print df.to_latex() 

這是非常相當於

print df.to_latex(formatters=formatters) 

並且與換位的數據幀以及df.T.to_latex()

相關問題