I would want to open an .CSV file with st.file_uploader and return a data frame. My code returns a non type object accompanied with an error message of
" AttributeError: ‘NoneType’ object has no attribute ‘iloc’"
Here is the function that I wrote for this task:
data_file = st.sidebar.file_uploader("Upload ELE.CSV" , type=['csv'])
def upload():
if data_file is not None:
df= pd.read_csv(data_file , header=None , sep=';' , encoding='unicode_escape' , skip_blank_lines=False)
return st.dataframe(df)
ELE = upload()
x0 = np.array(ELE.iloc[0 , 1:142].astype(float))
st.write(x0)
at the output I see the content of the file with the error message:
Your issue is here. The thing to keep in mind about st.dataframe is that it is a display object, it does not return a dataframe. You should just return df from your function.
Thank you very much @randyzwitch for your advice.
There is still one problem, I get the same error message before uploading the file. Once it is uploaded, everything works fine. Could you please let me know how to fix this?
Your code doesn’t work because ELE resolves to None for the function upload() on the first run. Then, you try to run np.array on that None object, which won’t work.
I would move the if data_file: statement outside of the upload() function and place it before the ELE creation line.