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

Is there a Python equivalent to SQL's "WHERE LIKE "%%"?

I am trying to add a new column into a Pandas DataFrame. I want it to be based on my ‘SKU’ column and basically say "if the SKU ends with -RF, write "USED" into the new column, elif SKU ends with -NEW, write "NEW" into the new column, else don’t do anything.

I know in my SQL you can write this like "…WHERE LIKE "%-RF", I’m looking for something that does the same job as that.

This was the (unfinished) function as I was attempting to make it. I’m trying to figure out what goes in place of the "something_to_do_with" placeholders.

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

    if sku_column == something_to_do_with_-RF:
        val = "USED"
    elif sku_column == something_to_do_with_-NEW:
        val = "NEW"
    else:
        continue

>Solution :

No need for a loop.

df.loc[df['sku'].str.endswith("-RF"),'column'] = 'USED'
df.loc[df['sku'].str.endswith("-NEW"),'column'] = 'NEW'
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