dataframe_image icon indicating copy to clipboard operation
dataframe_image copied to clipboard

Is there an option to drop the index in the image?

Open rlf037 opened this issue 4 years ago • 3 comments

Is this possible?

rlf037 avatar Jun 17 '21 12:06 rlf037

I had the same question. I am not sure of the correct final design here, and the below would be a DataFrame-only solution. It is possible this could be solved by Styler, but I don't know much about that yet. I would propose a new parameter to export to take a show_table_index parameter which could default to True and not break current design.

In the following file:

C:\Users\[username]\AppData\Roaming\Python\Python39\site-packages\dataframe_image\_pandas_accessor.py

I changed line 71 from:

        html = obj.to_html(max_rows=max_rows, max_cols=max_cols, notebook=True)

to:

        df = pd.DataFrame(obj)
        html = df.to_html(index=False, max_rows=max_rows, max_cols=max_cols, notebook=True)

and passing index=False to the to_html() call.

korygill avatar Jun 19 '21 19:06 korygill

Thanks. This did work for me as a temporary solution.

rlf037 avatar Jun 20 '21 10:06 rlf037

@korygill Thanks for this solution! I just tried it and it worked on my development machine. However, when I tried implementing it on my server, it broke my site (I suspect it was an issue with gunicorn). But that's an issue for a different thread.

trlemon avatar Aug 23 '22 22:08 trlemon

@korygill @trlemon I figured out how to drop the index and keep other styles as well (for example, I'm going to hide the index and apply a background gradient). Define df_styled prior to the export:

df_styled = df.style.hide().background_gradient()  
dfi.export(df_styled, 'df_styled.png')

The pandas documentation: https://pandas.pydata.org/docs/user_guide/style.html#Hiding-Data Suggests formatting like:

df_styled = df.style.hide() \
        .background_gradient()  
dfi.export(df_styled, 'df_styled.png')

thebeeric avatar Jul 29 '23 02:07 thebeeric