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