简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM