2

I have dataframe and I want update a value of a column of a dataframe for specific set of data. How can this be done.I have around 20000 records to be updated.

Sample Input

Id  Registered
345     Y
678     N
987     N
435     N
2345    Y
123     N
679     N

I want to update the Registered column to Y when I give Set of Id numbers . How this can be done I want to change the Registered column of 678,124,435 to Y . How this can this can done when for a large list.

2 Answers 2

9

You can use a mask generated with isin to index df's Registered column and set values accordingly.

df.loc[df['Id'].isin(ids), 'Registered'] = 'Y'
df

     Id Registered
0   345          Y
1   678          Y
2   987          N
3   435          Y
4  2345          Y
5   123          N
6   679          N
Sign up to request clarification or add additional context in comments.

2 Comments

@COLDSPEED, the IDs should be passed as a list right.
@Rahulrajan Bit late to answer but any container (Series, list, array, etc) will work, not just a list.
1

Or you can using where/mask

ids=[678,124,435]    
df.Registered=df.Registered.where(df.Id.isin(ids),'Y')

Or,

df.Registered=df.Registered.where(df.Id.isin(ids),'Y')

df

     Id Registered
0   345          Y
1   678          N
2   987          Y
3   435          N
4  2345          Y
5   123          Y
6   679          Y

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.