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
#1
Hi, as i`m new to python my code is not clean and not efficient and im looking towards faster performance while im loading my code.
Im using streamlit and pandas and i have filters like this 12 times for each filter option:
    # Create for foo
regionas = st.sidebar.multiselect("Pick your Regionas", options=df.sort_values(by="Regionas").Regionas.unique())
if not regionas:
    df2 = df.copy()
else:
    df2 = df[df["Regionas"].isin(regionas)]

x with each option available
if not regionas and not valstija and not marketas and not zmogus and not daiktas and not dienos and not menesiai and not seima and not augintinis and not miestas and not siurprizas:
    filtered_df = df
elif not valstija and not marketas and not zmogus and not daiktas and not dienos and not menesiai and not seima and not augintinis and not miestas and not siurprizas
    filtered_df = df[df["Regionas"].isin(regionas)]

then to step up filtering i add 2 filter option so i filtrate by 2 options instead of 1
elif valstija and regionas:
    filtered_df = df12[df["State"].isin(valstija) & df12["Regionas"].isin(regionas)]

after that i add 3 option i can choose from my filter and 4 and 5 and all the way to the end that i can choose all filters and my data will be filtered [ Example: A or B or C or D or E or F or G or H or I or J or K or L- then AB or AC or AD or AE or EF , EG or AG ... or KBC ADB or ABCD or DEFGH or ABGHJI or GHIABCD etc. .... and end looks like ABCDEFGHIJKL if i choose all. ]
Currently i have 8 filters working in specific order[ have to choose A if i want C but if i choose C it wont show A and i choose filter H its not filtering out my filter G but if i choose filter G i can choose filter H ] and it takes 300 LOC and it takes ages to read when i start streamlit app ( not considering uploading file and filtering ).
Where i have to start to clean , minimize my code and make it more efficient while keeping my 12 filter options and maybe i can add more in the future.
Im not really sure with it but i think if i have 12 filters and i want to filter out by any selection i have 12x12x11 options ...give a take ?
Thank You.
Reply


Messages In This Thread
Cleaning my code to make it more efficient - by BSDevo - Sep-26-2023, 10:46 AM

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,542 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