Pandas error = ValueError: Length of values (4) does not match length of index (1). in streamlit

dear Boss

I would like to display a three-level hierarchical header in a pandas DataFrame using Streamlit’s st.dataframe. Below is my code along with sample data; however, it results in a ValueError: Length of values (4) does not match length of index (1).
Could you please provide the correct code?

from turtle import pd

import streamlit as st

import pandas as pd

import numpy as np

labels = [

\["SEMESTERFall1"\],

\["Aiyana", "Aiyana", "Anisha", "Anisha"\],

\["Mathematics", "Science", "Mathematics", "Science"\]

]

tuples = list(zip(*labels))

#index = pd.MultiIndex.from_tuples(tuples, names=[“Students”, “Subjects”])

index = pd.MultiIndex.from_tuples(tuples, names=[“SEMESTER”,“Students”, “Subjects”])

df = pd.DataFrame([

\[98, 95, 99\],

\[95, 93, 96\],

\[92, 99, 95\],

\[99, 95, 97\]

], index=index, columns=[“1st term”, “2nd term”, “Final”])

# works as expected for a dataframe

st.dataframe(df)

# transposing… shows weird results

st.dataframe(df.T)

see error

ValueError: Length of values (4) does not match length of index (1)

File “D:\pyff\tt8.py”, line 20, in
df = pd.DataFrame([
[98, 95, 99],
…<2 lines>…
[99, 95, 97]
], index=index, columns=[“1st term”, “2nd term”, “Final”])
File “C:\Users\user\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\frame.py”, line 845, in _init_
mgr = arrays_to_mgr(
arrays,
…<2 lines>…
dtype=dtype,
)
File “C:\Users\user\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\internals\construction.py”, line 118, in arrays_to_mgr
arrays, refs = _homogenize(arrays, index, dtype)
~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\user\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\internals\construction.py”, line 597, in _homogenize
com.require_length_match(val, index)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
File “C:\Users\user\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\common.py”, line 601, in require_length_match
raise ValueError(
…<4 lines>…
)

Hey there, thanks for sharing your question and welcome to the community! :blush: The error “Length of values (4) does not match length of index (1)” happens because your MultiIndex has 4 rows, but your data only has 4 rows of 3 values each—so the shape doesn’t match the index and columns.

It could be possible that the issue is with the shape of your data and the index. For a MultiIndex with 4 rows, each row in your data should have 3 columns (matching your columns list), and there should be 4 rows in total. Here’s a corrected version that should work with Streamlit’s st.dataframe, displaying a three-level hierarchical header:

import streamlit as st
import pandas as pd

labels = [
    ["SEMESTERFall1", "SEMESTERFall1", "SEMESTERFall1", "SEMESTERFall1"],
    ["Aiyana", "Aiyana", "Anisha", "Anisha"],
    ["Mathematics", "Science", "Mathematics", "Science"]
]
tuples = list(zip(*labels))
index = pd.MultiIndex.from_tuples(tuples, names=["SEMESTER", "Students", "Subjects"])
df = pd.DataFrame(
    [[98, 95, 99], [95, 93, 96], [92, 99, 95], [99, 95, 97]],
    index=index,
    columns=["1st term", "2nd term", "Final"]
)
st.dataframe(df)

This should display your DataFrame with the desired hierarchical header in Streamlit. If you transpose, be aware that MultiIndex columns may not render as expected in Streamlit, as discussed in the docs and forum posts. Would you like more detail on MultiIndex support or transposing behavior?

Sources: