(Apr-20-2017, 04:46 PM)kiton Wrote: To create a new column with identifed FIPS code, I do the following:
for index, row in df.iterrow(): df["FIPS"] = df[stateplane.identify('lon', 'lat', fmt='fips')]
This code contains quite big amount of errors. Iterating over
df.iterrows()
(not df.iterrow()) means iterating over pairs (index, row) as you correctly use in for clausule, but you dont use it at all in the body of the loop... steteplane.identify()
seems to accept only numerical arguments for longitude and latitude, not 'lon'
and 'lat'
strings (it cant know that these should be substitued with values of appropriate row of dataframe). Morever indexing dataframe with returning value of identify would almost never work, neither trying to assign it to a new df column.It is possible to rewrite your for loop to work as you want:
df['FIPS'] = 0 # dummy value for index, row in df.iterrows(): df['FIPS'][index] = stateplane.identify(row['lon'], row['lat'], fmt='fips')But standard (and more effective) way to apply some function over dataframe columns or rows is to use
.apply()
:df['fips'] = df.apply(lambda row: stateplane.identify(row['lon'], row['lat'], fmt='FIPS'), axis=1)