St.dataframe displays int as float

Summary

I am trying to display a pandas dataframe but the columns containing integers are displayed as floats. (picture below)

The columns NPDCode and Year are supposed to be displayed as integers.

Expected behavior:

Here’s what I get when using the same function in jupyter notebook (in vscode)

Debug info

  • Streamlit version: latest
  • Python version: 3.11.1
  • OS version: Windows 10

It looks more like it is the common way of representing thousands in English, as you can also see further to the right.

Hi @Franky1 ! Thank you for your reply.

I get your point but is there a way of preventing that behaviour?

df[‘Year’]=df[‘Year’].astype(str)

Hi @ac3! Thank you for your reply.

I can’t really do that as I use the same dataframe later to plot the annual production (picture below). The x axis cannot be made of strings.

Note: The original dataset came unsorted. To sort it, I need to keep that column’s dtype as integer.

You could split up the dataframe into two dataframes, one for the table display and one for further numerical analysis. Not ideal, but a workaround.

@rimano You can use st.table(df)

Using st.table(df)

Using st.dataframe(df)
image

The difference is st.table() will show df as static table and st.dataframe() allow you to sort data.

1 Like

If displaying the dataframe is important (as I assume it is), then make a copy of the dataframe (using df.copy()) set it to a new variable, cast the dtype of this new variable to integer and display your plots from the new table.
This should help

Had the same issue seen numbers like this: 120,131,009,003,829.98 instead of like this: 0120131009003830 same as in .csv file.
To solve this problem I used pandas style to specify “commas” to separate thousands.

df.style.format(thousands=',')

I just removed comma between ' symbols to make it solid number :

st.dataframe(df.style.format(thousands=''))

2 Likes

Hi @atriumsplotch ! Thank you for your reply.

Your solution worked like a charm. However, I ended up doing something slightly different. I used the precision parameter as well.

Indeed, using the method you suggested led to having a bunch of unwanted zeros at the end (apparently, the default is 6 decimals).

styler.format.precision: default 6.

So I did this:

df.style.format(precision=0, thousands='')

And If needed, you can always get the full value by double clicking on the cell.

Result

Honestly, I didn’t even know you could style a dataframe so thanks again :slightly_smiling_face:.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.