pythonpandasdataframematplotlibpdfpages

How to export each row from two data frames (which share same identifier) in a separate pdf file?


I am strugling to find a way to compose a pdf for each row which would contained only one row from two data frames. Both dataframes has two common identifiers which are unique for given dataframe. Say I have these two data frames:

    country number  Particip    EvoParticip 
    CZ  2   731658.000000000    26.100000000
    CZ  4   696739.000000000    -13.200000000   
    CZ  5   580834.000000000    -10.500000000   

and

    country number  Trans EvoTrans
    CZ  2   1656171.000000000   12.900000000    
    CZ  4   16265.000000000 26.000000000    
    CZ  5   17302.000000000 26.200000000    

The desired result is to have for each row single pdf containing records from both dataframes like this:

    country number  Particip    EvoParticip 
    CZ  2   731658.000000000    26.100000000
    
    country number  Trans EvoTrans
    CZ  2   1656171.000000000   12.900000000    
    

So far I was able to produce a single pdf with complete dataframes but I don't know how exactly proceed to get desired result. Heres my code:

figs = [fig,fig1]
fig
the_table = ax.table(cellText=df2.values,colLabels=df2.columns,loc='center')

fig1
the_table = ax.table(cellText=df3.values,colLabels=df3.columns,loc='center')

with PdfPages("foo.pdf") as pp:
    for figy in figs:
        pp.savefig(figy, bbox_inches='tight')

Solution

  • You can adjust your code by creating multiple PdfPages objects :

    from matplotlib.backends.backend_pdf import PdfPages
    
    for i, (r1, r2) in enumerate(zip(df1.to_numpy(), df2.to_numpy()), 1):
        with PdfPages(f"file_{i}.pdf") as pp:
            fig, axes = plt.subplots(2, 1)
    
            for ax, (row, header) in zip(
                axes, [(r1, df1.columns), (r2, df2.columns)]):
                ax.table(
                    cellText=[row],
                    colLabels=header,
                    cellLoc="center",
                    loc="center"
                );ax.axis("off")
            
            pp.savefig(fig, bbox_inches="tight")
    

    NB : This assumes that the two dataframes are correctly sorted and have the same shape.

    enter image description here