I have a table whose header looks like this: complaint_type borough street_name incident_zip latitude longitude

I want to check if the "incident_zip" column of each row is in a specific list of zip codes and change the "borough" accordingly. There is a large amount of data and i cannot find any better code to do this. I am using python 3.6.

  • $\begingroup$ Hi, can you please add what have you tried so far? and why its not working? $\endgroup$
    – yoav_aaa
    Commented Nov 3, 2019 at 8:58
  • $\begingroup$ Hi! I have been using If and replace commands. But it is taking a lot of time for processing. I mean so much time that I have to interrupt the kernel. $\endgroup$
    – Platinum
    Commented Nov 4, 2019 at 12:07

1 Answer 1


There is a way that is not optimum for performance, but clear to read and understand. Also, you can use more complex logic and update with different values as you wish.

def myfunc(row):
    if row['incident_zip'] in [this is a list of zip codes]:
        return new_value
        return row['borough'] #return old value

data['borough'] = data.apply(myfunc, axis=1)

If your logic of update is quite simple, then you can do it like that

mask = df['incident_zip'].isin([this is a list of zip codes])
df[mask]['borough'] = new_value
  • $\begingroup$ Hi Tasos. Thanks for helping. I have five long lists of zip codes. That means the borough name which is 'unspecified' will get one of the five borough names. Is there any other way to determine the borough name for example 'latitude and longitude' $\endgroup$
    – Platinum
    Commented Nov 4, 2019 at 12:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.