A table and CSV download with only the chosen levels

Fill in the blanks · Basic · §11.3 Connecting charts and tables

The table parking holds the number of cars parked on each level of a parking lot each day. Show only the rows of the levels chosen in the sidebar as a table (st.dataframe), and let the user download the same rows as a CSV file. Leave the row labels (0, 1, 2, …) out of the CSV, and give the file type as the MIME type for CSV. Fill in the three blanks. Grading runs the app again with different levels and car counts.

Target figure for A table and CSV download with only the chosen levels
Target figure

Code with blanks

import pandas as pd
import streamlit as st

parking = pd.DataFrame({
    "day": ["Mon", "Mon", "Mon", "Tue", "Tue", "Tue"],
    "level": ["Basement", "Ground", "Roof"] * 2,
    "cars": [140, 210, 65, 152, 198, 80],
})
st.title("Parking lot")
levels = st.sidebar.multiselect("Level", ["Basement", "Ground", "Roof"], default=["Basement", "Ground", "Roof"])
view = parking[parking["level"].isin(levels)]
if view.empty:
    st.warning("Pick at least one level.")
    st.stop()

st.dataframe(____, hide_index=True)
csv = view.to_csv(____).encode("utf-8-sig")
st.download_button("Download CSV", csv, file_name="parking_view.csv", mime=____)

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.