How can I replace the data 'Beer','Alcohol','Beverage','Drink'
with only 'Drink'
.
df.replace(['Beer','Alcohol','Beverage','Drink'],'Drink')
doesn't work
It seems that your initial method of doing it works in the the latest iteration of Python.
df.replace(['Beer','Alcohol','Beverage','Drink'],'Drink', inplace=True)
Should work
You almost had it. You need to pass a dictionary to df.replace
.
df
Col1
0 Beer
1 Alcohol
2 Beverage
3 Drink
df.replace(dict.fromkeys(['Beer','Alcohol','Beverage','Drink'], 'Drink'))
Col1
0 Drink
1 Drink
2 Drink
3 Drink
This works for exact matches and replacements. For partial matches and substring matching, use
df.replace(
dict.fromkeys(['Beer','Alcohol','Beverage','Drink'], 'Drink'),
regex=True
)
This is not an in-place operation so don't forget to assign the result back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With