Changing the value of a column based on the condition on another column


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.


Posted 2019-11-03T03:29:23.050

Reputation: 11

Hi, can you please add what have you tried so far? and why its not working? – yoav_aaa – 2019-11-03T08:58:36.233

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. – Platinum – 2019-11-04T12:07:21.143



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


Posted 2019-11-03T03:29:23.050

Reputation: 3 340

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' – Platinum – 2019-11-04T12:02:41.260