Export a Pandas dataframe as a table image

前端 未结 4 975
独厮守ぢ
独厮守ぢ 2020-11-27 02:55

Is it possible to export a Pandas dataframe as an image file? Something like df.to_png() or df.to_table().savefig(\'table.png\').

At the mo

4条回答
  •  感情败类
    2020-11-27 03:23

    I had the same requirement for a project I am doing. But none of the answers came elegant to my requirement. Here is something which finally helped me, and might be useful for this case:

    from bokeh.io import export_png, export_svgs
    from bokeh.models import ColumnDataSource, DataTable, TableColumn
    
    def save_df_as_image(df, path):
        source = ColumnDataSource(df)
        df_columns = [df.index.name]
        df_columns.extend(df.columns.values)
        columns_for_table=[]
        for column in df_columns:
            columns_for_table.append(TableColumn(field=column, title=column))
    
        data_table = DataTable(source=source, columns=columns_for_table,height_policy="auto",width_policy="auto",index_position=None)
        export_png(data_table, filename = path)
    

提交回复
热议问题