How to save a pandas DataFrame table as a png
Pandas allows you to plot tables using matplotlib (details here).
Usually this plots the table directly onto a plot (with axes and everything) which is not what you want. However, these can be removed first:
import matplotlib.pyplot as plt
import pandas as pd
from pandas.table.plotting import table # EDIT: see deprecation warnings below
ax = plt.subplot(111, frame_on=False) # no visible frame
ax.xaxis.set_visible(False) # hide the x axis
ax.yaxis.set_visible(False) # hide the y axis
table(ax, df) # where df is your data frame
plt.savefig('mytable.png')
The output might not be the prettiest but you can find additional arguments for the table() function here.
Also thanks to this post for info on how to remove axes in matplotlib.
EDIT:
Here is a (admittedly quite hacky) way of simulating multi-indexes when plotting using the method above. If you have a multi-index data frame called df that looks like:
first second
bar one 1.991802
two 0.403415
baz one -1.024986
two -0.522366
foo one 0.350297
two -0.444106
qux one -0.472536
two 0.999393
dtype: float64
First reset the indexes so they become normal columns
df = df.reset_index()
df
first second 0
0 bar one 1.991802
1 bar two 0.403415
2 baz one -1.024986
3 baz two -0.522366
4 foo one 0.350297
5 foo two -0.444106
6 qux one -0.472536
7 qux two 0.999393
Remove all duplicates from the higher order multi-index columns by setting them to an empty string (in my example I only have duplicate indexes in "first"):
df.ix[df.duplicated('first') , 'first'] = '' # see deprecation warnings below
df
first second 0
0 bar one 1.991802
1 two 0.403415
2 baz one -1.024986
3 two -0.522366
4 foo one 0.350297
5 two -0.444106
6 qux one -0.472536
7 two 0.999393
Change the column names over your "indexes" to the empty string
new_cols = df.columns.values
new_cols[:2] = '','' # since my index columns are the two left-most on the table
df.columns = new_cols
Now call the table function but set all the row labels in the table to the empty string (this makes sure the actual indexes of your plot are not displayed):
table(ax, df, rowLabels=['']*df.shape[0], loc='center')
et voila:
Your not-so-pretty but totally functional multi-indexed table.
EDIT: DEPRECATION WARNINGSAs pointed out in the comments, the import statement for table
:
from pandas.tools.plotting import table
is now deprecated in newer versions of pandas in favour of:
from pandas.plotting import table
EDIT: DEPRECATION WARNINGS 2The ix
indexer has now been fully deprecated so we should use the loc
indexer instead. Replace:
df.ix[df.duplicated('first') , 'first'] = ''
with
df.loc[df.duplicated('first') , 'first'] = ''
Can I save a table/dataframe to a file (like png/jpg) in python?
Assuming this table is a pandas DataFrame, this library might help:
www.dexplo.org/dataframe_image/
This library would export pandas DataFrames in a jupyter notebook fashioned way.
Example usage:
import pandas as pd
import dataframe_image as dfi
df = pd.DataFrame({'key':[1,2,3],'val':['a','b','c']})
dfi.export(df, 'dataframe.png')
Save pandas table (filled with strings) as png
First of all your columns lists have to be the same length. You may plot table with matplotlib and table
function from pandas.
import pandas as pd
import matplotlib.pylab as plt
from pandas.tools.plotting import table
# I add None value to align all lists
column1 = ['Measured Set', '1. set', '2. set', '3. set']
column2= ['Breached parameter (number of breaches, %)', None, None,None ]
column3 = ['Breached parameter (number of breaches, %)', None, None,None]
data = {
'Sensor': column1,
'Sensor 1': column2,
'Sensor 2': column3,
}
df = pd.DataFrame(data)
print(df)
# set fig size
fig, ax = plt.subplots(figsize=(12, 3))
# no axes
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
# no frame
ax.set_frame_on(False)
# plot table
tab = table(ax, df, loc='upper right')
# set font manually
tab.auto_set_font_size(False)
tab.set_fontsize(8)
# save the result
plt.savefig('table.png')
How to save the Pandas dataframe/series data as a figure?
Option-1: use matplotlib table functionality, with some additional styling:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame()
df['date'] = ['2016-04-01', '2016-04-02', '2016-04-03']
df['calories'] = [2200, 2100, 1500]
df['sleep hours'] = [8, 7.5, 8.2]
df['gym'] = [True, False, False]
def render_mpl_table(data, col_width=3.0, row_height=0.625, font_size=14,
header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='w',
bbox=[0, 0, 1, 1], header_columns=0,
ax=None, **kwargs):
if ax is None:
size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
fig, ax = plt.subplots(figsize=size)
ax.axis('off')
mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, **kwargs)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)
for k, cell in mpl_table._cells.items():
cell.set_edgecolor(edge_color)
if k[0] == 0 or k[1] < header_columns:
cell.set_text_props(weight='bold', color='w')
cell.set_facecolor(header_color)
else:
cell.set_facecolor(row_colors[k[0]%len(row_colors) ])
return ax.get_figure(), ax
fig,ax = render_mpl_table(df, header_columns=0, col_width=2.0)
fig.savefig("table_mpl.png")
Options-2 Use Plotly + kaleido
import plotly.figure_factory as ff
import pandas as pd
df = pd.DataFrame()
df['date'] = ['2016-04-01', '2016-04-02', '2016-04-03']
df['calories'] = [2200, 2100, 1500]
df['sleep hours'] = [8, 7.5, 8.2]
df['gym'] = [True, False, False]
fig = ff.create_table(df)
fig.update_layout(
autosize=False,
width=500,
height=200,
)
fig.write_image("table_plotly.png", scale=2)
fig.show()
For the above, the font size can be changed using the font
attribute:
fig.update_layout(
autosize=False,
width=500,
height=200,
font={'size':8}
)
How I can save big tables as image with pandas
I realize you mentioned image, but I would look into trying to make it into an html page. Luckily, pandas can handle all of it for us with the df.to_html()
method. Definitely take a look at the documentation which allows lots of nifty options (html classes, ids, borders, etc etc).
But as an example, it would look something like this:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(1538, 4),
columns=['a', 'b', 'c', 'd'])
with open('dataframe.html', 'w') as outfile:
outfile.write(df.to_html())
Then you can just open up dataframe.html
in any browser.
Related Topics
Splitting One CSV into Multiple Files
Adding a Data File in Pyinstaller Using the Onefile Option
Filenotfounderror: [Errno 2] No Such File or Directory
How to Make Python Get the Username in Windows and Then Implement It in a Script
Masking User Input in Python With Asterisks
Join Two Dataframes from a Conditional Row
Python - Get Path of Root Project Structure
Skip First Couple of Lines While Reading Lines in Python File
How to Plot Date and Time in X Axis Against Y Value (Python)
How to Get the Previous Element When Using a for Loop
Convert CSV to Parquet File Using Python
How to Create a Multiline Plot Using Seaborn
How to Iterate Through a List of Dictionaries in Jinja Template
Python: Read Text File and Split File into List Variables, With Each Variable Having 4 Lines Each
How to Split But Ignore Separators in Quoted Strings, in Python
How to Concatenate/Append Multiple Spark Dataframes Column Wise in Pyspark