python - Export a Pandas dataframe as a table image -



python - Export a Pandas dataframe as a table image -

is possible export pandas dataframe image file? df.to_png() or df.to_table().savefig('table.png').

at moment export dataframe using df.to_csv(). open csv file in excel create info pretty , re-create / paste excel table powerpoint image. see matplotlib has .table() method, i'm having problem getting work df.

the df i'm using has 5 columns & 5 rows , each 'cell' number.

thanks in advance.

if have pdflatex , imagemagick installed, export dataframe tex, utilize pdflatex convert pdf file, , convert pdf png using imagemagick:

import pandas pd import numpy np import subprocess df = pd.dataframe({'d': [1., 1., 1., 2., 2., 2.], 'c': np.tile(['a', 'b', 'c'], 2), 'v': np.arange(1., 7.)}) filename = 'out.tex' pdffile = 'out.pdf' outname = 'out.png' template = r'''\documentclass[preview]{{standalone}} \usepackage{{booktabs}} \begin{{document}} {} \end{{document}} ''' open(filename, 'wb') f: f.write(template.format(df.to_latex())) subprocess.call(['pdflatex', filename]) subprocess.call(['convert', '-density', '300', pdffile, '-quality', '90', outname])

if install phantomjs , imagemagick, export dataframe html , utilize phantomjs convert html png, , imagemagick crop result:

import pandas pd import numpy np import subprocess df = pd.dataframe({'d': [1., 1., 1., 2., 2., 2.], 'c': np.tile(['a', 'b', 'c'], 2), 'v': np.arange(1., 7.)}) filename = '/tmp/out.html' outname = '/tmp/out.png' cropname = '/tmp/cropped.png' open(filename, 'wb') f: f.write(df.to_html()) rasterize = '/path/to/phantomjs/examples/rasterize.js' subprocess.call(['phantomjs', rasterize, filename, outname]) subprocess.call(['convert', outname, '-trim', cropname])

python pandas

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -