helpwithAI
helpwithAI

Reputation: 3

Pandas change value based on other column values

I want to change the value of each item in the column 'ageatbirth' to '1' if the 'race' is 2. In pseudocode:

If 'race' == 2 and 'ageatbirth' == 2:

'ageatbirth' == 1 

Is there an easy way to do this for a very large dataset?

Upvotes: 0

Views: 39

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 29982

Use

m = df['race'].eq(2) & df['ageatbirth'].eq(2)

df['ageatbirth'] = df['ageatbirth'].mask(m, 1)
# or
df.loc[m, 'ageatbirth'] = 1

Upvotes: 1

Related Questions