Rahul rajan
Rahul rajan

Reputation: 1266

How to update a column for list of values in Pandas Dataframe

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.

Upvotes: 2

Views: 8857

Answers (2)

cs95
cs95

Reputation: 403278

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

Upvotes: 9

BENY
BENY

Reputation: 323396

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

Upvotes: 1

Related Questions