我有一个表,其标题如下所示:complaint_type borough street_name event_zip latitude longitude
我想检查每行的“incident_zip”列是否在特定的邮政编码列表中,并相应地更改“自治市镇”。有大量数据,我找不到更好的代码来做到这一点。我正在使用python 3.6。
我有一个表,其标题如下所示:complaint_type borough street_name event_zip latitude longitude
我想检查每行的“incident_zip”列是否在特定的邮政编码列表中,并相应地更改“自治市镇”。有大量数据,我找不到更好的代码来做到这一点。我正在使用python 3.6。
有一种方法不是性能最佳,但易于阅读和理解。此外,您可以使用更复杂的逻辑并根据需要使用不同的值进行更新。
def myfunc(row):
if row['incident_zip'] in [this is a list of zip codes]:
return new_value
else:
return row['borough'] #return old value
data['borough'] = data.apply(myfunc, axis=1)
如果您的更新逻辑很简单,那么您可以这样做
mask = df['incident_zip'].isin([this is a list of zip codes])
df[mask]['borough'] = new_value