How to save the Pandas dataframe/series data as a figure?

前端 未结 7 1797
猫巷女王i
猫巷女王i 2020-11-29 00:25

It sounds somewhat weird, but I need to save the Pandas console output string to png pics. For example:

>>> df
                   sales  net_pft             


        
7条回答
  •  萌比男神i
    2020-11-29 00:54

    I was interested saving my dataframe as a table for an appendix for a report. I found this to be the simplest solution:

    import pandas as pd
    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    
    # Assuming that you have a dataframe, df
    pp = PdfPages('Appendix_A.pdf')
    total_rows, total_cols = df.shape; #There were 3 columns in my df
    
    rows_per_page = 40; # Assign a page cut off length
    rows_printed = 0
    page_number = 1;
    
    while (total_rows >0): 
        #put the table on a correctly sized figure    
        fig=plt.figure(figsize=(8.5, 11))
        plt.gca().axis('off')
        matplotlib_tab = pd.tools.plotting.table(plt.gca(),df.iloc[rows_printed:rows_printed+rows_per_page], 
            loc='upper center', colWidths=[0.2, 0.2, 0.2])    
    
        # Give you cells some styling 
        table_props=matplotlib_tab.properties()
        table_cells=table_props['child_artists'] # I have no clue why child_artists works
        for cell in table_cells:
            cell.set_height(0.024)
            cell.set_fontsize(12)
    
        # Add a header and footer with page number 
        fig.text(4.25/8.5, 10.5/11., "Appendix A", ha='center', fontsize=12)
        fig.text(4.25/8.5, 0.5/11., 'A'+str(page_number), ha='center', fontsize=12)
    
        pp.savefig()
        plt.close()
    
        #Update variables
        rows_printed += rows_per_page;
        total_rows -= rows_per_page;
        page_number+=1;
    
    pp.close()
    

提交回复
热议问题