Upload and merge multiple CSV files to one DataFrame

Hey there, great job on now having the accept_multiple_files flag for file_uploader. However, I am having an issue when I want to upload multiple CSV files, and then concatenate these files into one DataFrame.

uploaded_files = st.file_uploader("Upload CSV", type="csv", accept_multiple_files=True)
if uploaded_files:
    uploaded_data_read = [pd.read_csv(file) for file in uploaded_files]
    raw_data = pd.concat(uploaded_data_read)

returns

EmptyDataError: No columns to parse from file

for the line [pd.read_csv(file) for file in uploaded_files].

I don’t get issues when I upload one file, only when I upload more than one file.

Here is the output of st.write(uploaded_files) after uploading two files

[
  "<class 'streamlit.uploaded_file_manager.UploadedFile'>",
  "<class 'streamlit.uploaded_file_manager.UploadedFile'>"
]

Can anyone help?

Try:

uploaded_files = st.file_uploader("Upload CSV", type="csv", accept_multiple_files=True)
if uploaded_files:
    for file in uploaded_files:
        file.seek(0)
    uploaded_data_read = [pd.read_csv(file) for file in uploaded_files]
    raw_data = pd.concat(uploaded_data_read)

The UploadedFile class is a subclass of BytesIO, and therefore it is “file-like”. This means you can pass them anywhere where a file is expected. As a subclass of BytesIO, make sure to reset the buffer after reading it with UploadedFile.seek(0).

1 Like