Search code examples
pythonpython-3.xpandasstringdata-cleaning

Replacing a string value in one column based upon a list of other values in another column


I am cleaning a dataset and I need to replace the string in column A with "foo" if column B contains one of the strings from a list of options.

I have a list of values contained in column B that I want to use to indicate that I need to replace column A

A   B
foo apple
foo banana
bar cherry
foo orange
bar melon
bar papaya

I have multiple values that could trigger a replacement.

list = ['papaya', 'avocado', 'cherry', 'mango']

using that list to inform the replacement I want to change the value in column A to foo if column B contains one of the values in the list. The results in this example would look like this.

A   B
foo apple
foo banana
foo cherry
foo orange
bar melon

Any advice helps, thanks.


Solution

  • Do:

    lst = ['papaya', 'avocado', 'cherry', 'mango']
    df.loc[df['B'].isin(lst), 'A'] = 'foo'
    print(df)
    

    Output

         A       B
    0  foo   apple
    1  foo  banana
    2  foo  cherry
    3  foo  orange
    4  bar   melon
    5  foo  papaya