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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.