Summary
Hello everyone,
I鈥檓 facing a peculiar issue while using st.session_state
with Pandas DataFrames. I鈥檝e set up a session state variable as a DataFrame to hold some course information. I鈥檓 trying to append a new row to this DataFrame, but I鈥檓 getting an AttributeError: 'DataFrame' object has no attribute 'append'
.
Steps to Reproduce
Code snippet:
pythonCopy code
import pandas as pd
import streamlit as st
if 'df' not in st.session_state:
st.session_state.df = pd.DataFrame()
def add_new_course():
if st.session_state.df.empty:
st.warning("No data available to add a new course.")
return
new_row = pd.Series({
'finish': 95,
'work': None,
'exam': None,
'weight': 3,
'semester': 'a',
'type': 'ba',
'year': 1,
'course_name': 'Intro to Python',
'difficulty': None
})
st.session_state.df = st.session_state.df.append(new_row, ignore_index=True)
- Initialize
st.session_state.df
as an empty Pandas DataFrame. - Call the
add_new_course()
function.
Expected behavior:
I expect a new row to be appended to the DataFrame stored in st.session_state.df
.
Actual behavior:
I get an AttributeError: 'DataFrame' object has no attribute 'append'
. Debugging shows that st.session_state.df
is indeed a Pandas DataFrame, making the error message confusing.
Links
- GitHub repo: GitHub - edenede2/mean_cal_app
- Deployed app: (Replace with your deployed app link)
Additional information
The error occurs only when I try to append a new row to the DataFrame stored in st.session_state
.