在图像或 pdf 文档中将 pandas 数据帧保存为 table,具有良好的多索引显示
Save a pandas dataframe as table in Image or pdf document with nice multi index display
我正在尝试在 pdf 格式的报告中包含一个具有多索引的数据框。我想要一个不错的 table 输出。
我找到了这两个解决方案:
pandas.df -> HTML -> 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 -> 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")
然而,我不熟悉乳胶,我得到这个错误:
! LaTeX Error: File `standalone.cls' not found.
Type X to quit or <RETURN> to proceed,
or enter a new name. (Default extension: cls)
关于错误或标准方法的任何想法 pandas.df -> pdf ?
好吧,一种方法是使用 markdown。您可以使用 df.to_html()
。这会将数据帧转换为 html table。从那里您可以将生成的 html 放入 markdown 文件 (.md) 并使用包将 markdown 转换为 pdf。
https://www.npmjs.com/package/markdown-pdf
这是一个不错的选择吗?
适合我的解决方案:
pandas >= 0.17
我安装了pdflatex。我复制了 booktabs.sty、geography.sty 和 pdflscape.sty
等乳胶包
import pandas as pd
import os
import math
def save_summary_table_as_pdf(path_to_csv, path_to_output_folder):
pwd = os.getcwd()
df = pd.read_csv(path_to_csv, sep =',')
#data preparation
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
nbpages = int(math.ceil(res1.shape[0]*1.0/40))
templatetop = r'''\documentclass[a3paper, 5pt]{article}
\usepackage{booktabs}
\usepackage{pdflscape}
\usepackage[a4paper,bindingoffset=0.2in,%
left=0.25in,right=0.25in,top=1in,bottom=1in,%
footskip=.25in]{geometry}
\begin{document}
\begin{landscape}
\pagenumbering{gobble}
\oddsidemargin = 0pt
\hoffset = -0.25in
\topmargin = 1pt
\headheight = 0pt
\headsep = 0pt
'''
templatebottom = '''
\end{landscape}
\end{document}
'''
output_folder_path_abs = path_to_output_folder
output_tex = os.path.join(output_folder_path_abs,
"clustering_summary_table.tex")
with open(output_tex, "wb") as afile:
afile.write(templatetop +'\n')
for i in range(0, nbpages):
afile.write(res1.iloc[(i*40):((i+1)*40), :].to_latex() +'\n' +
"""\pagenumbering{gobble}""")
afile.write(templatebottom +'\n')
os.chdir(output_folder_path_abs)
os.system('pdflatex clustering_summary_table.tex')
os.chdir(pwd)
os.remove(output_tex)
os.remove(os.path.join(path_to_output_folder,
'clustering_summary_table.aux'))
os.remove(os.path.join(path_to_output_folder,
'clustering_summary_table.log'))
if __name__ == "__main__":
print 'begin generate pdf table about clustering'
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("path_to_csv")
parser.add_argument("outputfolder")
args = vars(parser.parse_args())
filedir = os.path.abspath(os.path.dirname(__file__))
output_folder_path_abs = os.path.abspath(args['outputfolder'])
input_folder_path_abs = os.path.abspath(args['path_to_csv'])
# copy the user package latex to the folder
os.system('scp '
+os.path.abspath(os.path.join(filedir, 'userpackagelatex/booktabs.sty'))+
' ' +output_folder_path_abs)
os.system('scp '
+os.path.abspath(os.path.join(filedir, 'userpackagelatex/geography.sty'))+
' ' +output_folder_path_abs)
os.system('scp '
+os.path.abspath(os.path.join(filedir, 'userpackagelatex/pdflscape.sty'))+
' ' +output_folder_path_abs)
save_summary_table_as_pdf(input_folder_path_abs, output_folder_path_abs)
os.remove(os.path.join(output_folder_path_abs, 'booktabs.sty'))
os.remove(os.path.join(output_folder_path_abs, 'geography.sty'))
os.remove(os.path.join(output_folder_path_abs, 'pdflscape.sty'))
我正在尝试在 pdf 格式的报告中包含一个具有多索引的数据框。我想要一个不错的 table 输出。
我找到了这两个解决方案:
pandas.df -> HTML -> 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 -> 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")
然而,我不熟悉乳胶,我得到这个错误:
! LaTeX Error: File `standalone.cls' not found.
Type X to quit or <RETURN> to proceed,
or enter a new name. (Default extension: cls)
关于错误或标准方法的任何想法 pandas.df -> pdf ?
好吧,一种方法是使用 markdown。您可以使用 df.to_html()
。这会将数据帧转换为 html table。从那里您可以将生成的 html 放入 markdown 文件 (.md) 并使用包将 markdown 转换为 pdf。
https://www.npmjs.com/package/markdown-pdf
这是一个不错的选择吗?
适合我的解决方案: pandas >= 0.17 我安装了pdflatex。我复制了 booktabs.sty、geography.sty 和 pdflscape.sty
等乳胶包import pandas as pd
import os
import math
def save_summary_table_as_pdf(path_to_csv, path_to_output_folder):
pwd = os.getcwd()
df = pd.read_csv(path_to_csv, sep =',')
#data preparation
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
nbpages = int(math.ceil(res1.shape[0]*1.0/40))
templatetop = r'''\documentclass[a3paper, 5pt]{article}
\usepackage{booktabs}
\usepackage{pdflscape}
\usepackage[a4paper,bindingoffset=0.2in,%
left=0.25in,right=0.25in,top=1in,bottom=1in,%
footskip=.25in]{geometry}
\begin{document}
\begin{landscape}
\pagenumbering{gobble}
\oddsidemargin = 0pt
\hoffset = -0.25in
\topmargin = 1pt
\headheight = 0pt
\headsep = 0pt
'''
templatebottom = '''
\end{landscape}
\end{document}
'''
output_folder_path_abs = path_to_output_folder
output_tex = os.path.join(output_folder_path_abs,
"clustering_summary_table.tex")
with open(output_tex, "wb") as afile:
afile.write(templatetop +'\n')
for i in range(0, nbpages):
afile.write(res1.iloc[(i*40):((i+1)*40), :].to_latex() +'\n' +
"""\pagenumbering{gobble}""")
afile.write(templatebottom +'\n')
os.chdir(output_folder_path_abs)
os.system('pdflatex clustering_summary_table.tex')
os.chdir(pwd)
os.remove(output_tex)
os.remove(os.path.join(path_to_output_folder,
'clustering_summary_table.aux'))
os.remove(os.path.join(path_to_output_folder,
'clustering_summary_table.log'))
if __name__ == "__main__":
print 'begin generate pdf table about clustering'
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("path_to_csv")
parser.add_argument("outputfolder")
args = vars(parser.parse_args())
filedir = os.path.abspath(os.path.dirname(__file__))
output_folder_path_abs = os.path.abspath(args['outputfolder'])
input_folder_path_abs = os.path.abspath(args['path_to_csv'])
# copy the user package latex to the folder
os.system('scp '
+os.path.abspath(os.path.join(filedir, 'userpackagelatex/booktabs.sty'))+
' ' +output_folder_path_abs)
os.system('scp '
+os.path.abspath(os.path.join(filedir, 'userpackagelatex/geography.sty'))+
' ' +output_folder_path_abs)
os.system('scp '
+os.path.abspath(os.path.join(filedir, 'userpackagelatex/pdflscape.sty'))+
' ' +output_folder_path_abs)
save_summary_table_as_pdf(input_folder_path_abs, output_folder_path_abs)
os.remove(os.path.join(output_folder_path_abs, 'booktabs.sty'))
os.remove(os.path.join(output_folder_path_abs, 'geography.sty'))
os.remove(os.path.join(output_folder_path_abs, 'pdflscape.sty'))