Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamic for loop function for filtering df based on provided parameters

If I wanted to create a new df that only had rows from the original df that fall into specified categories, what would be the most efficient way to do that?

df = sns.load_dataset('diamonds')

def makenewdf(cuts=['Ideal','Premium'], df=df):
[some kind of loop to dynamically filter df based on the values of cuts]

what would be the best way to make this function such that I could provide the categories I want to sequester?

ex: makenewdf(cuts = ['Good']) would return a df containing only rows where the cut was ‘Good’ and makenewdf(cuts = ['Good','Ideal','Premium']) would return a df with only rows containing one of the three values in cuts

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re searching for the isin() function, you can use something like this:

def makenewdf(cuts, df):
    return df[df.cut.isin(cuts)]

# Example
print(makenewdf(['Good'], df))

# Example
print(makenewdf(['Good','Ideal','Premium'], df))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading