Filtering columns by header name

In my code I need to be able to filter column names(on the left component) and show only filtered columns with information after user uploaded a CSV file. This is my code but I am stuck on it at the moment and have no ideas of how I can implement the requirements.

try:
    from enum import Enum
    from io import BytesIO, StringIO
    from typing import Union

    import pandas as pd
    import streamlit as st
except Exception as e:
    print(e)

STYLE = """
<style>
img {
    max-width: 100%;
    width: 900
}
</style>
"""


class FileUpload(object):

    def __init__(self):
        self.fileTypes = ["csv", "png", "jpg"]

    def run(self):
        """
        Upload File on Streamlit Code
        :return:
        """
        # st.info(__doc__)
        st.markdown(STYLE, unsafe_allow_html=True)
        st.sidebar.title("Filter data")
        st.set_option('deprecation.showfileUploaderEncoding', False)
        file = st.file_uploader("Upload file", type=self.fileTypes)
        st.write(file)
        show_file = st.empty()
        if not file:
            show_file.info("Please upload a file of type: " +
                           ", ".join(["csv", "png", "jpg"]))
            return
        content = file.getvalue()
        if isinstance(file, BytesIO):
            show_file.image(file)
        else:
            data = pd.read_csv(file)
            st.dataframe(data.head(10))
        file.close()


if __name__ == "__main__":
    helper = FileUpload()
    helper.run()

Hi @darkness49,

I think you can use multiselect with column names for this purpose, here’s a minimal code sample for the same.

import streamlit as st
import pandas as pd

# create some dataframe
df = pd.DataFrame({f"f_{i}": list(range(100)) for i in range(10)})

filtered = st.multiselect("Filter columns", options=list(df.columns), default=list(df.columns))

st.write(df[filtered])
2 Likes