0

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.

Mitchell.Laferla
  • 221
  • 2
  • 12
  • 1
    Does this answer your question? [How to filter Pandas dataframe using 'in' and 'not in' like in SQL](https://stackoverflow.com/questions/19960077/how-to-filter-pandas-dataframe-using-in-and-not-in-like-in-sql) – Bill Huang Dec 02 '20 at 21:24

1 Answers1

1

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
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76