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
CodePudding user response:
No need for a loop.
df.loc[df['sku'].str.endswith("-RF"),'column'] = 'USED'
df.loc[df['sku'].str.endswith("-NEW"),'column'] = 'NEW'
CodePudding user response:
The equivalent of SQL's joker %a_text
in pandas is pandas.Series.str.endswith
(a_text).
In your case, it might be better to use pandas.Series.str.extract
with a regex to capture the desired text and map it with a pre-defined dictionnary.
Try this :
dico = {'RF': 'USED', 'NEW': 'NEW'}
df['new_col'] = df['SKU'].str.extract('.-(RF|NEW)$', expand=False).map(dico)