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.
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'