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>…
)
