How can I replace the data 'Beer','Alcohol','Beverage','Drink'
with only 'Drink'
.
df.replace(['Beer','Alcohol','Beverage','Drink'],'Drink')
doesn't work
How can I replace the data 'Beer','Alcohol','Beverage','Drink'
with only 'Drink'
.
df.replace(['Beer','Alcohol','Beverage','Drink'],'Drink')
doesn't 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.
Try the following approach:
lst = ['Beer','Alcohol','Beverage','Drink']
pat = r"\b(?:{})\b".format('|'.join(lst))
df = df.replace(pat, 'Drink', regexp=True)
Looks like different from MaxU's solution :)
df.replace({'|'.join(['Beer','Alcohol','Beverage','Drink']):'Drink'},regex=True)
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
Slight change in earlier answers: Following code Replacing values of specific column/Columns
df[['Col1']] = df[['Col1']].replace(dict.fromkeys(['Beer','Alcohol','Beverage','Drink'], 'Drink'))
Not relevant to your exact case using strings, but relevant to the question as stated in the title:
If you have a range of numbers you would like to replace you can provide range()
as a dict key.
# Dymmy dataframe with two columns
df = pd.DataFrame(dict(a=[1, 2, 3, 4, 5], b=[66, 44, 33, 22, 77]))
# Mapping that will replace 1 with 'x', 2, 3, 4 with 'y' and 5 with 'z' in column 'a'
mapping = {"a": {1: "x", range(2, 5): "y", 5: "z"}}
df.replace(mapping)