i have the below dataframe that have one field tech includes string value that are separated with - i want to make this field as tags where once the user select one of these values it display the datafarme grouped by the project_title.
code:
import streamlit as st
import pandas as pd
import numpy as np
data = {
'project_title': ['LSE', 'DCP', 'Job-detection', 'Task management & Organizer'],
'tech': ['python-RegExp-PyQt5', 'python-RegExp', 'python-RegExp-BeautifulSoup-pandas', 'python-pandas-MS_SQL-CSS/HTML-Javascript'],
'Role': ['Junior developer ', 'Python developer', 'Python developer', 'Tech lead']
}
#split the tech column into multiple columns
df[['tech1','tech2','tech3','tech4','tech5']]=df['tech'].str.split('-', expand=True)
#create seperated list of each tech column
tech1 = df["tech1"].unique().tolist()
tech2 = df["tech2"].unique().tolist()
tech3 = df["tech3"].unique().tolist()
tech4 = df["tech4"].unique().tolist()
tech5 = df["tech5"].unique().tolist()
#concatinate all the lists into one list.
tech_all = tech1+tech2+tech3+tech4+tech5
tech_all = list(filter(None, tech_all))
#create multiselect widget that includes the created list of tech
regular_search_term =tech_all
choices = st.multiselect(" ",regular_search_term)
#return the dataframe based on the selected values from the multiselect widget.
df_result_search=df[df.loc[:,"tech1":"tech5"].isin(choices)]
st.write(df_result_search)
The above code did not return what i want as result
It sounds like you want to put ['python-RegExp-PyQt5', 'python-RegExp', 'python-RegExp-BeautifulSoup-pandas', 'python-pandas-MS_SQL-CSS/HTML-Javascript']
into a multiselect. I’m exactly sure about what
“display the datafarme grouped by the project_title”
means but maybe you can clarify what that means.
Thanks for posting and hope to hear from you soon.
i am trying to make the tech column as a tag for each project_title where the user will select value/s from the multiselect widget and based on the selected value the app display the related records where it includes all the fields.
the line below i am trying to expand the one column tech into multiple columns in order to create a list of it. df[['tech1','tech2','tech3','tech4','tech5']]=df['tech'].str.split('-', expand=True)
you can use explode instead of expand it seems better way.
seems like you want to have python,RegExp, BeautifulSoup,Pandas,PyQt5, MS_SQL, CSS/HTML, and javascript into a multiselect and then when they select any of those options in the multiselect, you return the df based on options that are selected?
If that’s true, I suggest looping through all of the techs and then splitting by “-” and then adding them to a set (guarantees unique).
However, I am confused how you get the returned dataframes. are those combinations of all of the tech, project titlem and role?
what i want if its possible to separate the tech in the multiselect widget and return the same result.
Does this mean you want two multiselect widgets? What do you mean separate the tech in the multiselect widget?
I think I understand your question, I do it a lot for all of my apps
So I created this handy function:
You provide:
A dataframe
A list of columns to create widgets from (if not provided it uses all columns)
Set “allow_single_value_widgets” to True if you want to have widgets for columns that only have one unique value in the table
def filters_widgets(df, columns=None, allow_single_value_widgets=False):
# Parse the df and get filter widgets based for provided columns
if not columns: #if columns not provided, use all columns to create widgets
columns=df.columns.tolist()
if allow_single_value_widgets:
threshold=0
else:
threshold=1
widget_dict = {}
filter_widgets = st.container()
filter_widgets.warning(
"After selecting filters press the 'Apply Filters' button at the bottom.")
if not allow_single_value_widgets:
filter_widgets.markdown("Only showing columns that contain more than 1 unique value.")
with filter_widgets.form(key="data_filters"):
not_showing = []
for y in df[columns]:
if str(y) in st.session_state: #update value from session state if exists
selected_opts = st.session_state[str(y)]
else: #if doesnt exist use all values as defaults
selected_opts = df[y].unique().tolist()
if len(df[y].unique().tolist()) > threshold: #checks if above threshold
widget_dict[y] = st.multiselect(
label=str(y),
options=df[y].unique().tolist(),
default=selected_opts,
key=str(y),
)
else:#if doesnt pass threshold
not_showing.append(y)
if not_showing:#if the list is not empty, show this warning
st.warning(
f"Not showing filters for {' '.join(not_showing)} since they only contain one unique value."
)
submit_button = st.form_submit_button("Apply Filters")
#reset button to return all unselected values back
reset_button = filter_widgets.button(
"Reset All Filters",
key="reset_buttons",
on_click=reset_filter_widgets_to_default,
args=(df, columns),
)
filter_widgets.warning(
"Dont forget to apply filters by pressing 'Apply Filters' at the bottom."
)
And also:
def reset_filter_widgets_to_default(df, columns):
for y in df[columns]:
if str(y) in st.session_state:
del st.session_state[y]
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.