I have two issues one its expecting the file to be uploaded when the form is opened and get the error
NameError: name ‘df’ is not defined
until i upload the file then the error disappears. thats one issue i cant’t seem to solve.
The other is how can I upload either a csv or excel file and get the program to automatically tell what was uploaded and read the dataframe? I have tried .endswith and .seek(0) but to no avail.
It would help if you could share a code snippet that leads to these errors. It will make it easier for us to debug.
NameError: name ‘df’ is not defined
The above error is likely caused due to how you’ve structured your code. I’m guessing you’re trying to use the DataFrame df before it is created by reading your uploaded file. A possible solution is to:
Use an if statement to check if the file has been uploaded
Read the uploaded file into a Dataframe df within the if block
Perform computations on df within the if block.
The other is how can I upload either a csv or excel file and get the program to automatically tell what was uploaded
st.file_uploader has a type argument that accepts an array of allowed file extensions. In your case, it would be type=['csv', 'xlsx']. You can once again use if/else statements to check the MIME type of the uploaded file and use the corresponding pandas method to create df.
Here’s an example:
Code
import streamlit as st
import pandas as pd
# Allow only .csv and .xlsx files to be uploaded
uploaded_file = st.file_uploader("Upload spreadsheet", type=["csv", "xlsx"])
# Check if file was uploaded
if uploaded_file:
# Check MIME type of the uploaded file
if uploaded_file.type == "text/csv":
df = pd.read_csv(uploaded_file)
else:
df = pd.read_excel(uploaded_file)
# Work with the dataframe
st.dataframe(df.head())
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.