Is there a way I can right align the numbers in streamlit dataframe
Cast them as a string and they will left align.
No any other method? Converting them to string makes sorting of values on the basis of ASCII.
This is possible with the pandas style API, but Streamlit doesn’t fully support it, you can try the code here.
Here’s how to left align columns using pandas:
import pandas as pd
# Create a DataFrame with fake data
data = {
'Category': ['A', 'B', 'C', 'D', 'E'],
'Values': [10, 15, 20, 25, 30]
}
data_df = pd.DataFrame(data)
# Left align the numbers in the 'Values' column using pandas style
styled_df = data_df.style.set_table_styles([dict(selector='th', props=[('text-align', 'left')])])
styled_df = styled_df.set_properties(**{'text-align': 'left'})
styled_df
Result:
However, this doesn’t work in Streamlit:
I mean the heading is aligned at one side and the values are aligned to another side. Looks pretty ugly. I wanted to use st.table but it adds that automatic index and If I make my own df column index, the heading of the column is removed.
import pandas as pd
# Create a DataFrame with fake data
data = {
'Category': ['A', 'B', 'C', 'D', 'E'],
'Values': [1000, 1500, 2000, 2500, 3000]
}
data_df = pd.DataFrame(data)
import streamlit as st
st.table(data_df)
data_df.set_index('Category', inplace=True)
st.table(data_df)