user_df
user_df

Reputation: 43

How to filter a dataframe and update selective rows for a particular cell

I want to update a particular column in DF when certain condition on another column passes . this is what I am trying but it gives error

train[train['Rain]'==1]['Price']=100

So for all rows when Rain column is 1 , for that Row the price column should be set to 100 , could you give an example of using it via where and without where fucntion as well.

Upvotes: 1

Views: 44

Answers (2)

Infinite
Infinite

Reputation: 764

You could use below three options

train.loc[train['Rain'] == 1, ['Price']] = 100

OR

import numpy as np
train['Price'] = np.where(train['Rain'] == 1, 100,train['Price'])

OR use the 'at' operator

train.at[train['Rain'] == 1, ['Price']] = 100

Hope that helps

Upvotes: 1

Adnan Umer
Adnan Umer

Reputation: 3689

Use Dataframe.loc

train.loc[train['Rain'] == 1, ['Price']] = 100

Upvotes: 0

Related Questions