Carmen
Carmen

Reputation: 793

Replace values in column based on condition, then return dataframe

I'd like to replace some values in the first row of a dataframe by a dummy.

df[[0]].replace(["x"], ["dummy"])

The problem here is that the values in the first column are replaced, but not as part of the dataframe.

print(df)

yields the dataframe with the original data in column 1. I've tried

df[(df[[0]].replace(["x"], ["dummy"]))]

which doesn't work either..

Upvotes: 1

Views: 516

Answers (1)

EdChum
EdChum

Reputation: 394459

replace returns a copy of the data by default, so you need to either overwrite the df by self-assign or pass inplace=True:

df[[0]].replace(["x"], ["dummy"], inplace=True)

or

df[0] = df[[0]].replace(["x"], ["dummy"])

see the docs

Upvotes: 2

Related Questions