S. Caruso
S. Caruso

Reputation: 39

How to replace row data based off the value

I am trying to find a way so that when i load a csv file into a pandas df I can run a script that will go through the Description column and change the value if a condition is met. Example, if the the value in the description column has the word x in it, will then replace that value with y and continue through that column and do it for multiple values.

Im not sure if this would be better an if..elif..else statement or better to locate buy row and then build a function.

data.loc[(data.Description == 'x'),'Description']= 'y'

But im not sure of the code to do a like statement in sql, for pandas. I was trying data.loc['indexes'] == data.Description.str.contains("x"), 'Description' = 'y' but i couldnt get it to work

Upvotes: 0

Views: 230

Answers (2)

wasif
wasif

Reputation: 15518

Try with .str.contains():

data.loc[(data['Description'].str.contains('x')),'Description'] = 'Description'

Upvotes: 2

BENY
BENY

Reputation: 323376

You can check with

data.loc[data.Description.str.contains("x"), 'Description'] = 'y'

Upvotes: 4

Related Questions