简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM