Export Pandas DataFrame into a PDF file using Python

后端 未结 4 1453
既然无缘
既然无缘 2020-12-01 04:42

What is an efficient way to generate PDF for data frames in Pandas?

4条回答
  •  温柔的废话
    2020-12-01 05:25

    First plot table with matplotlib then generate 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()
    

    reference:

    How do I plot only a table in Matplotlib?

    Reduce left and right margins in matplotlib plot

提交回复
热议问题