0

I have the script below I typed in Pycharm for my Streamlit Data app:

import pandas as pd
import plotly.express as px
import streamlit as st

st.set_page_config(page_title='Matching Application Number', 
                   layout='wide')
df = pd.read_csv('Analysis_1.csv')

st.sidebar.header("Filter Data:")
MeetingFileType = st.sidebar.multiselect(
    "Select File Type:",
    options=df['MEETING_FILE_TYPE'].unique(),
    default=df['MEETING_FILE_TYPE'].unique()
)

df_selection = df.query(
    'MEETING_FILE_TYPE == @MeetingFileType'
)

st.dataframe(df_selection)

The output of this on streamlit is:

Application_ID    MEETING_FILE_TYPE
BBC#:1010         1     
NBA#:1111         2
BRC#:1212         1
SAC#:1412         4
QRD#:1912         2
BBA#:1092         4

How can I return matching Application_ID results just for 1&2 like below:

Filter Data:               Application_ID     MEETING_FILE_TYPE
select type:               BBC#:1010          1 
1 2                        NBA#:1111          2
                           BRC#:1212          1
                           QRD#:1912          2

Then, how can I download the data results above from Streamlit into a csv file?, Thank you.

Romeo Botanpi
  • 51
  • 1
  • 6

2 Answers2

2

Have a look on the code comments.

Code

...

st.dataframe(df_selection)

# Show download button for the selected frame.
# Ref.: https://docs.streamlit.io/library/api-reference/widgets/st.download_button
csv = df_selection.to_csv(index=False).encode('utf-8')
st.download_button(
     label="Download data as CSV",
     data=csv,
     file_name='selected_df.csv',
     mime='text/csv',
 )

Streamlit output

enter image description here

Downloaded csv file selected_df.csv when viewed from excel.

enter image description here

Full code

I am using your code.

import pandas as pd
import plotly.express as px
import streamlit as st

data = {
    "Application_ID": ['BBC#:1010', 'NBA#:1111', 'BRC#:1212',
                       'SAC#:1412', 'QRD#:1912', 'BBA#:1092'],
    "MEETING_FILE_TYPE": [1, 2, 1, 4, 2, 4]
}


st.set_page_config(page_title='Matching Application Number', 
                   layout='wide')
# df = pd.read_csv('Analysis_1.csv')
df = pd.DataFrame(data)

st.sidebar.header("Filter Data:")
MeetingFileType = st.sidebar.multiselect(
    "Select File Type:",
    options=df['MEETING_FILE_TYPE'].unique(),
    default=df['MEETING_FILE_TYPE'].unique()
)

df_selection = df.query(
    'MEETING_FILE_TYPE == @MeetingFileType'
)

st.dataframe(df_selection)

# Show download button for the selected frame.
# Ref.: https://docs.streamlit.io/library/api-reference/widgets/st.download_button
csv = df_selection.to_csv(index=False).encode('utf-8')
st.download_button(
     label="Download data as CSV",
     data=csv,
     file_name='selected_df.csv',
     mime='text/csv',
 )
ferdy
  • 4,396
  • 2
  • 4
  • 16
  • Thank you.. What scripts did you use to filter the data, so that it will only return rows that have only 1&2 File Type. – Romeo Botanpi Apr 16 '22 at 17:29
  • I use your code. Answer is updated, full code is added. – ferdy Apr 17 '22 at 09:10
  • Thank you very much for your help!! The only problem is it is giving me all the Application_ID for MEETING_FILE_TYPE 1 + all the Application_ID for MEETING_FILE_TYPE 2. I just want it to return only the Application_ID that MEETING_FILE_TYPE 1 & 2 have in common. I want to define my "MeetingFileType" as a joint function that will only return matching join Application_IDs that are MEETING_FILE_TYPE 1 and MEETING_FILE_TYPE 2 at the same time. Thank you!! – Romeo Botanpi Apr 17 '22 at 20:36
  • Like for instance, to make it clear, I am going to use SQL language to explain my question: I am looking to do an INNER JOIN that will return all the records Application_ID that have matching values in both MEETING_FILE_TYPE 1 & 2 rows from my Analysis_1.csv file, Thank you so much. – Romeo Botanpi Apr 17 '22 at 22:13
1

I think you can use csv module to export your output as csv file

import csv

with open('filename.csv') as csvfile:
   writer = csv.writer(csvfile)
   writer.writerow(df_selection)