Home > database >  AttributeError: 'str' object has no attribute 'latitude'
AttributeError: 'str' object has no attribute 'latitude'

Time:01-22

I am trying to add a new column "Coordinates" to the csv file with the use of pandas library.

I am trying to include only the latitudes in this column using lambda. But I am getting the error. If there are any other alternatives except the lambda to do it, please mention them too. Thank You

Here is my code:

df10["Points"] = df10["Address"].apply(lambda x: x.latitude)

The dataframe containing data

Here is the error:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21272/1836949934.py in <module>
----> 1 df10["Points"] = df10["Address"].apply(lambda x: x.latitude)

~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwargs)
   4355         dtype: float64
   4356         """
-> 4357         return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
   4358 
   4359     def _reduce(

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply(self)
   1041             return self.apply_str()
   1042 
-> 1043         return self.apply_standard()
   1044 
   1045     def agg(self):

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
   1096                 # List[Union[Callable[..., Any], str]]]]]"; expected
   1097                 # "Callable[[Any], Any]"
-> 1098                 mapped = lib.map_infer(
   1099                     values,
   1100                     f,  # type: ignore[arg-type]

~\anaconda3\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

~\AppData\Local\Temp/ipykernel_21272/1836949934.py in <lambda>(x)
----> 1 df10["Points"] = df10["Address"].apply(lambda x: x.latitude)

AttributeError: 'str' object has no attribute 'latitude'

CodePudding user response:

Check for type in column Address, also u can have nulls in the column.

CodePudding user response:

It looks like your address is a string rather than an object with a latitude property. Trying printing x and see for yourself.

  •  Tags:  
  • Related