在 Pandas 中为数据框生成 PDF 的有效方法是什么?
最佳答案
首先使用 matplotlib
绘制表格,然后生成 pdf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))
#https://stackoverflow.com/questions/32137396/how-do-i-plot-only-a-table-in-matplotlib
fig, ax =plt.subplots(figsize=(12,4))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')
#https://stackoverflow.com/questions/4042192/reduce-left-and-right-margins-in-matplotlib-plot
pp = PdfPages("foo.pdf")
pp.savefig(fig, bbox_inches='tight')
pp.close()
引用:
关于python - 使用 Python 将 Pandas DataFrame 导出为 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155776/