Skip to content
Advertisement

How to add prefix to the selected records in python pandas df

I have df where some of the records in the column contains prefix and some of them not. I would like to update records without prefix. Unfortunately, my script adds desired prefix to each record in df:

new_list = [] 
prefix = 'x' 
for ids in df['ids']:
        if ids.find(prefix) < 1:
            new_list.append(prefix + ids)

How can I ommit records with the prefix? I’ve tried with df[df[‘ids’].str.contains(prefix)], but I’m getting an error.

Advertisement

Answer

Use Series.str.startswith for mask and add values with numpy.where:

df = pd.DataFrame({'ids':['aaa','ssx','xwe']})

prefix = 'x' 

df['ids'] = np.where(df['ids'].str.startswith(prefix), '', prefix) + df['ids']

print (df)
    ids
0  xaaa
1  xssx
2   xwe
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement