How to use precision with st.experimental_data_editor()?

 if 'exp_data_frame' not in st.session_state:
    df2 = df2.style.format(precision=0)
    st.session_state.exp_data_frame = st.experimental_data_editor(df2)
    output_df = st.session_state.exp_data_frame
else:
    output_df = st.experimental_data_editor(
    st.session_state.exp_data_frame
        )

I have above code, however, the precision with integers not working.

could anyone please help to fix the code, how I can have the precision set with edit option as well?

Hi @raghava4u

I’ve created 2 lines for rounding the numbers using round()., please see the code below:

  1. `df2 = df.round(2) will apply this to all columns.
  2. df2 = df.round({'MolLogP': 3, 'MolWt': 2}) will selectively apply to specific columns.
import streamlit as st
import pandas as pd

if 'exp_data_frame' not in st.session_state:
    df = pd.read_csv('https://raw.githubusercontent.com/dataprofessor/data/master/delaney_solubility_with_descriptors.csv')
    #df2 = df.round(2)
    df2 = df.round({'MolLogP': 3, 'MolWt': 2})
    st.session_state.exp_data_frame = st.data_editor(df2)
    output_df = st.session_state.exp_data_frame
else:
    output_df = st.data_editor(
    st.session_state.exp_data_frame
        )

Hope this helps!

Best regards,
Chanin

Thank you @dataprofessor . i have run the code given above for thest. but I still see the precision.

my requirement is like, I have a table, which a production serial number, which displayed as
image

i want to get id of ‘,’ and display full number

regards
Raghava.

Hi,

You can change the numbers shown below inside the round() method to 0:

The number defines the number of decimal places such as 3 and 2 as used in the above code example for MolLogP and MolWt, which you can change to 0.

Best regards,
Chanin

@dataprofessor , Thank you for your support, I tired using df.round(0) in my code but it failing. And today I hosted my code and you can view it in https://stdataeditor.streamlit.app/Dashboard

and GitHub - raghava4u/st.webapp: testing st.data_editor feature using streamlit to review the code.

appreciate your help!..

Hi, from the code you’re applying round() with 0 to the Serial number column and in the deployed app, the values under the Serial number column is showing no decimal places, so it seems to already work.

Best regards,
Chanin

@dataprofessor, Thank you for your response. finally I understood and fixed the issue…
this code block gave me the required results.

if 'exp_data_frame' not in st.session_state:
    st.session_state.exp_data_frame = st.data_editor(df2, column_config={
        "Serial Number": st.column_config.NumberColumn(
            format="%i",)
    },
    #hide_index=True,
)
    output_df = st.session_state.exp_data_frame
else:
    output_df = st.data_editor(
        st.session_state.exp_data_frame,  column_config={
            "Serial Number": st.column_config.NumberColumn(
                format="%i",)
    },
)