Screen Link:
https://app.dataquest.io/m/136/data-cleaning-walkthrough/13/parsing-geographic-coordinates-for-schools
My Code:
def find_lat(loc):
coords = re.findall("\(.+\)", loc)
lat = coords.split(",")[0].replace("(", "")
return lat
What I expected to happen:
I expected it to work.
What actually happened:
It throws an error
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-4999a1653547> in <module>
7
8
----> 9 data['hs_directory']['lat']=data['hs_directory']['Location 1'].apply(lat)
10 data['hs_directory']['lat'][0]
11
/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
3846 else:
3847 values = self.astype(object).values
-> 3848 mapped = lib.map_infer(values, f, convert=convert_dtype)
3849
3850 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-1-4999a1653547> in lat(col)
3 def lat (col):
4 coords=re.findall("\(.+\)",col)
----> 5 lat=coords.split(",")[0].replace("(","")
6 return lat
7
AttributeError: 'list' object has no attribute 'split'```
In the below solution why is [0] being used after coords list, because the list hasn’t been split into 2 coordinates, the split happens after the use of the split function
import re
def find_lat(loc):
coords = re.findall("\(.+\)", loc)
lat = coords*[0]*.split(",")[0].replace("(", "")
return lat