私はマルチインデックスのデータフレームをpdfのレポートに含めることを試みています。私は素敵なテーブル出力をしたいと思います。 > HTML - - > PDFpandasデータフレームを画像またはpdf文書に保存して素敵なマルチインデックス表示
import pandas as pd
from IPython.display import HTML
import pdfkit
# df generation
df = pd.read_csv(path_to_csv, sep =',')
groupeddf = df.groupby('Cluster')
res = groupeddf.describe([0.05, 0.5, 0.95])
res.index.rename(['Cluster', 'stats'], inplace=True)
res['Cluster'] = res.index.get_level_values('Cluster')
res['stats'] = res.index.get_level_values('stats')
populations = (res.iloc[(res.index.get_level_values('stats') == 'count'), \
0].values).tolist()
res['population'] = [populations[i] for i in res.index.labels[0].values()]
total_pop = sum(populations)
res['frequency'] =(res['population']/total_pop).round(3)
res.set_index(['Cluster', 'population','frequency', 'stats'], inplace=True)
res1 = res.iloc[(res.index.get_level_values('stats') == '5%') |
(res.index.get_level_values('stats') == 'mean') |
(res.index.get_level_values('stats') == '50%') |
(res.index.get_level_values('stats') == '95%')]
res1 = res1.round(2)
# saving the df
h = HTML(res1.to_html())
my_file = open('test.html', 'w')
my_file.write(h.data)
my_file.close()
options = {
'orientation': 'Landscape'
}
with open('test.html') as f:
pdfkit.from_file(f, 'out.pdf', options=options)
しかし、これは私たちにそれを困難にするpdfkit
への依存性を持っている
pandas.df:
私はこれらの2の解決策を発見しました。
! LaTeX Error: File `standalone.cls' not found.
Type X to quit or <RETURN> to proceed,
or enter a new name. (Default extension: cls)
: - > TEX - > PDFしかし、私はラテックスに慣れていない、と私はこのエラーを取得する(
Export a Pandas dataframe as a table imageで述べたように)
import pandas as pd
import os
# df generation
df = pd.read_csv(path_to_csv, sep =',')
groupeddf = df.groupby('Cluster')
res = groupeddf.describe([0.05, 0.5, 0.95])
res.index.rename(['Cluster', 'stats'], inplace=True)
res['Cluster'] = res.index.get_level_values('Cluster')
res['stats'] = res.index.get_level_values('stats')
populations = (res.iloc[(res.index.get_level_values('stats') == 'count'), \
0].values).tolist()
res['population'] = [populations[i] for i in res.index.labels[0].values()]
total_pop = sum(populations)
res['frequency'] =(res['population']/total_pop).round(3)
res.set_index(['Cluster', 'population','frequency', 'stats'], inplace=True)
res1 = res.iloc[(res.index.get_level_values('stats') == '5%') |
(res.index.get_level_values('stats') == 'mean') |
(res.index.get_level_values('stats') == '50%') |
(res.index.get_level_values('stats') == '95%')]
res1 = res1.round(2)
res1.rename(columns=lambda x: x.replace('_', ' '), inplace=True)
#latex
template = r'''\documentclass[preview]{{standalone}}
\usepackage{{booktabs}}
\begin{{document}}
{}
\end{{document}}
'''
with open("outputfile.tex", "wb") as afile:
afile.write(template.format(res1.to_latex()))
os.system("pdflatex outputfile.tex")
私はpandas.dfを使用しようとしています理由です
pandas.df - > pdfを実行する際のエラーまたは標準的な方法についてのご意見はありますか?
私が書いた最初のソリューション(DF - はHTML> - > PDFファイル)私のマシンで作業している、しかし、コードができません私がPDFkitをインストールする権利を持っていない遠方のマシンを走らせてください。私はmakedown-pdfと同じになると思う。だからこそ私は可能な限り依存関係を制限する必要があるのです。 –
markdown-pdfはpdfのサイズを変更できないため、テーブルのみが含まれています – Chaoste