Suppose I have a simple pandas dataframe and I want to show these values inside a web app using streamlit:
import pandas as pd
import streamlit as st
df = pd.DataFrame({'a': [1174505943511396352, 2414501743231376356]})
st.table(df)
Printing the dataframe gives me:
>>> df
a
0 1174505943511396352
1 2414501743231376356
But the result inside the web app would be shown like below:
As you can see the values are not correct, and they are somehow rounded and the original value is not displayed.
What have I done?
- I tried changing the
dtypeof my dataframe, but it did not work:
df['a'] = df['a'].astype(np.uint64)
st.table(df) # no changes!
- I also tried
style.format, it did not work either:
func = lambda x: np.uint64(x)
df = df.style.format({"a": func}
st.table(df) # I got errors as below
Error message:
Traceback (most recent call last):
File "c:\users\a.tabarayi\desktop\analyze-clustering-results\venv\lib\site-packages\streamlit\script_runner.py", line 349, in _run_script
exec(code, module.__dict__)
File "C:\Users\a.tabarayi\Desktop\analyze-clustering-results\src\app.py", line 90, in <module>
st.table(x)
File "c:\users\a.tabarayi\desktop\analyze-clustering-results\venv\lib\site-packages\streamlit\elements\data_frame.py", line 120, in table
marshall_data_frame(data, table_proto)
File "c:\users\a.tabarayi\desktop\analyze-clustering-results\venv\lib\site-packages\streamlit\elements\data_frame.py", line 150, in marshall_data_frame
File "c:\users\a.tabarayi\desktop\analyze-clustering-results\venv\lib\site-packages\streamlit\elements\data_frame.py", line 169, in _marshall_styles
translated_style = styler._translate()
TypeError: _translate() missing 2 required positional arguments: 'sparse_index' and 'sparse_cols'
Any help would be appreciated, thanks!
Edit:
The following solution seems to be working for me, but let me know if there is a better approach to prevent unnecessary type casting!
df['a'] = df['a'].astype(str)
st.table(df)
