Reputation: 313
I have a data frame with different columns.One column x has value "a" and other column y has value "b", can I replace both values simultaneously using replace?
I can do it by two statments like
df["x"]=df["x"].replace("a","not a")
df["y"]=df["y"].replace("b","not b")
It works fine but can I do it in one statement like -
df["x","y"]=df["x","y"].replace(["a","b"],["not a","not b"]) ?
Upvotes: 0
Views: 90
Reputation: 8273
To index by multiple columns, create a list inside of the indexing operator. Note that the replace method on a DataFrame will replace every occurrence in all the columns (so it may behave differently if you have 'a' values in both columns but only wanted to replace in one of them).
df[['x', 'y']] = df[['x', 'y']].replace({'a':'not a','b':'not b'})
Upvotes: 1