Python Forum
Cleaning my code to make it more efficient
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cleaning my code to make it more efficient
#14
(Sep-27-2023, 09:36 PM)deanhystad Wrote: This is shorter, but it is still messy. The less you have to type, the better the code. If you see that you are going to do the same thing over and over, use program tools designed to repeat something over and over, like a loop. Once you get rid of all the extra dataframes, the only difference between filtering by Region and Market are the names of the columns and the prompt in the sidebar. The rest of the code is the same, and easily moved to a loop. In the code below, I put the column name:sidebar prompt information in a dictionary.
# These are the columns we filter by.  column name: sidebar prompt.
# Could be list of column names if the column names always matched the prompt.
filter_columns = {
    "Region": "Region",
    "Market": "Market",
    "State": "State",
    "Zmogus": "Person",
    "Daiktas": "Thing",
    "Weekday": "Day",
    "Month": "Month",
    "Surprise": "Surprise",
    "Shop": "Shop",
    "Selma": "Family",
    "Type": "Type",
}


st.sidebar.header("Choose your filter")

# Loop through all the columns
df2 = df.copy()
for column, prompt in filter_columns.items():
    choices = st.sidebar.multiselect(f"Pick your {prompt}", options=sorted(set(df2[column].values)))
    if choices:
        df2 = df2[df2[column].isin(choices)]
To add, remove, change a sorting column all you need to do is edit the dictionary.

Your code with dictionary is way more pleasing, i will play with it more and try to understand it because if i choose State as an example - it filters everything what is after the State but not whats above it ( market and region ) [ in sidebar selection mode ]
Also im trying to use less df[numbers] - i like naming it like filtered_df etc as its easier for me to track what i have done and what i have defined.
Thank You again for your time and patience
Reply


Messages In This Thread
RE: Cleaning my code to make it more efficient - by BSDevo - Sep-27-2023, 10:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 851 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 731 Oct-22-2023, 09:08 PM
Last Post: tronic72
  A more efficient code titanif 2 519 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  how to make bot that sends instagram auto password reset code kraixx 2 1,454 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,172 Dec-03-2022, 10:07 PM
Last Post: Extra
  Making a function more efficient CatorCanulis 9 1,944 Oct-06-2022, 07:47 AM
Last Post: DPaul
  Apply textual data cleaning to several CSV files ErcoleL99 0 867 Jul-09-2022, 03:01 PM
Last Post: ErcoleL99
  Make the code shorter quest 2 1,541 Mar-14-2022, 04:28 PM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,846 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Pyspark - my code works but I want to make it better Kevin 1 1,821 Dec-01-2021, 05:04 AM
Last Post: Kevin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020