While there is no direct method to center the contents of a table, you can achieve the desired alignment of text within the dataframe by converting it to an HTML-based table using the following code:
import pandas as pd
import streamlit as st
# Intialize a list of tuples containing the CSS styles for table headers
th_props = [('font-size', '14px'), ('text-align', 'left'),
('font-weight', 'bold'),('color', '#6d6d6d'),
('background-color', '#eeeeef'), ('border','1px solid #eeeeef'),
]
# Intialize a list of tuples containing the CSS styles for table data
td_props = [('font-size', '14px'), ('text-align', 'center')]
# Define hover props for table data and headers
cell_hover_props = [('background-color', '#eeeeef')]
headers_props = [('text-align','center'), ('font-size','1.1em')]
# Aggregate styles in a list
styles = [
dict(selector="th", props=th_props),
dict(selector="td", props=td_props),
dict(selector="td:hover",props=cell_hover_props),
dict(selector='th.col_heading',props=headers_props),
dict(selector='th.col_heading.level0',props=headers_props),
dict(selector='th.col_heading.level1',props=td_props)
]
# Create a sample DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [11, 12, 13], 'D': [14, 15, 16]}
dataframe = pd.DataFrame(data)
# Use markdown to show the table as HTML as opposed to the streamlit version table
st.markdown(dataframe.T.style.set_table_styles(styles).to_html(), unsafe_allow_html=True)
I hope this helps.