Python Forum
Iterating through rows and saving result in a new column
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating through rows and saving result in a new column
#1
Hello everyone! I am seeking your help on the following task.

So, thankfully to you I was able to extract geo coordinates from a tweet, and save latitude/longitude as float64 in separate columns. Now, I need to convert coordinates to FIPS codes. The easiest way seems to be using -stateplane-:

import stateplane
stateplane.identify(-78.873165, 39.207769, fmt='fips') # lon first
'4701'
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')]
But I guess I am doing it incorrectly -- i.e., specifying lon/lat columns to get values from, as I get the following error:

Error:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/mymac/anaconda/lib/python3.6/site-packages/shapely/geometry/point.py in geos_point_from_py(ob, update_geom, update_ndim) 201 # From array protocol --> 202 array = ob.__array_interface__ 203 assert len(array['shape']) == 1 AttributeError: 'tuple' object has no attribute '__array_interface__' During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-20-e8453e36b340> in <module>() 1 for index, row in test.iterrows(): ----> 2 text["FIPS"] = stateplane.identify('lon', 'lat', fmt='fips') # lon then lat /Users/mymac/anaconda/lib/python3.6/site-packages/stateplane/stateplane.py in identify(lon, lat, fmt, statefp, countyfp) 83 ''' 84 ---> 85 result = _id(lon, lat, statefp=statefp, countyfp=countyfp) 86 87 try: /Users/mymac/anaconda/lib/python3.6/site-packages/stateplane/stateplane.py in _id(lon, lat, statefp, countyfp) 63 raise ValueError("SPCS not found for statefp={}".format(statefp)) 64 ---> 65 target = Point(lon, lat) 66 result = None 67 /Users/mymac/anaconda/lib/python3.6/site-packages/shapely/geometry/point.py in __init__(self, *args) 47 BaseGeometry.__init__(self) 48 if len(args) > 0: ---> 49 self._set_coords(*args) 50 51 # Coordinate getters and setters /Users/mymac/anaconda/lib/python3.6/site-packages/shapely/geometry/point.py in _set_coords(self, *args) 127 self._geom, self._ndim = geos_point_from_py(args[0]) 128 else: --> 129 self._geom, self._ndim = geos_point_from_py(tuple(args)) 130 131 coords = property(BaseGeometry._get_coords, _set_coords) /Users/mymac/anaconda/lib/python3.6/site-packages/shapely/geometry/point.py in geos_point_from_py(ob, update_geom, update_ndim) 233 coords = ob 234 n = len(coords) --> 235 dx = c_double(coords[0]) 236 dy = c_double(coords[1]) 237 dz = None TypeError: must be real number, not str
Please advise how to do it right.
Reply


Messages In This Thread
Iterating through rows and saving result in a new column - by kiton - Apr-20-2017, 04:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 6 1,842 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  Saving the print result in a text file Calli 8 2,177 Sep-25-2022, 06:38 PM
Last Post: snippsat
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 983 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,712 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Iterating Through Data Frame Rows JoeDainton123 2 3,075 Aug-09-2021, 07:01 AM
Last Post: Pedroski55
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,210 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,594 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  How to filter out Column data From Multiple rows data? firaki12345 10 5,444 Feb-06-2021, 04:54 AM
Last Post: buran
  how to combine rows to a column base on ids zhujp98 0 1,573 Nov-03-2020, 04:10 PM
Last Post: zhujp98
  How to generate rows based on values in a column to fill missing values codesmatter 1 2,215 Oct-31-2020, 12:05 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020