Home > other >  How to prevent plotly.figure_factory.create_table from transforming integers into float values?
How to prevent plotly.figure_factory.create_table from transforming integers into float values?

Time:12-19

import plotly.figure_factory as ff

def save_dataframe_as_image(df):

    fig =  ff.create_table(df)
    fig.write_image("image.png", scale=2)

The final image then contains only floats. How can I prevent the transformation from integer values into floats?

Before: enter image description here

After: enter image description here

CodePudding user response:

This is a workaround, but you can cast df as a string when you pass it to your save_dataframe_as_image function.

This ensures the values in the table will display to the same precision as the df without actually modifying your df.

For example:

import pandas as pd
import plotly.figure_factory as ff

def save_dataframe_as_image(df):
    fig =  ff.create_table(df)
    fig.write_image("image.png", scale=2)

df = pd.DataFrame({
    'SKU': [5,6,7],
    'Current Price': [24.7,65.22,149.99],
    'Sales': [45,15,15],
    'Return':[43,10,1],
    'Loss':[22,4,0],
    'Stock':[33,23,75]
})

save_dataframe_as_image(df.astype(str))

enter image description here

  • Related