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
#7
How about something like this:
for column in df:
    choices = set(df[column].values)
    if len(choices) < 2:
        continue
    choices = st.sidebar.multiselect(f"Pick your {column}", sorted(choices))
    if choices:
        df = df[df[column].isin(choices)]
This loops through every column. If there is more than one value to choose from in the column, it does the sidebar thing to get the user selections. If the user makes a selection, the selection is applied to the dataframe, removing the non-matching rows.

It is difficult to know if this is a good fit for what you are trying to accomplish, because I really don't understand your code. You provide such narrow view, focusing on something that I don't think you even need to do. Could you explain what your program is supposed to do? Why would somebody use your code? What steps would the follow to produce the results they want, and what are those results?
BSDevo likes this post
Reply


Messages In This Thread
RE: Cleaning my code to make it more efficient - by deanhystad - Sep-26-2023, 11:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 940 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 791 Oct-22-2023, 09:08 PM
Last Post: tronic72
  A more efficient code titanif 2 559 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  how to make bot that sends instagram auto password reset code kraixx 2 1,521 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,212 Dec-03-2022, 10:07 PM
Last Post: Extra
  Making a function more efficient CatorCanulis 9 2,035 Oct-06-2022, 07:47 AM
Last Post: DPaul
  Apply textual data cleaning to several CSV files ErcoleL99 0 902 Jul-09-2022, 03:01 PM
Last Post: ErcoleL99
  Make the code shorter quest 2 1,592 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,884 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Pyspark - my code works but I want to make it better Kevin 1 1,880 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