How to replace a string based upon the value in a different pandas column

Viewed 38

I am cleaning a dataset and I need to remove formatting errors in column A if the value in column B matches a specific string.

A       B
foo//,  cherry
bar//,  orange
bar//,  cherry
bar     apple

So in this situation if column B is 'cherry' I want to replace "//," with "," in column A. The final result would look like this.

A       B
foo,    cherry
bar//,  orange
bar,    cherry
bar     apple

Any advice is much appreciated

2 Answers

You can simply write a function that takes in a row as series, checks the cherry condition, fixes the string with str.replace and returns the row. The you can use df.apply over axis=1.

def fix(s):
    if s['B']=='cherry':
        s['A']=s['A'].replace('//,',',')
    return s

df.apply(fix, axis=1)
        A       B
0    foo,  cherry
1  bar//,  orange
2    bar,  cherry
3     bar   apple

I would first check which rows contain cherry in the B column:

rows = df['B'].str.contains('cherry')

and then replace "//" with "" in these rows but A column.

Related