I have a DataFrame where one column contains a dictionary. I am trying to return the first value from the dictionary by using apply and a lambda function but I keep getting an error related to floats. This is confusing because I have a dictionary not a float that I am trying to iterate through.
Error message example: 'float' object has no attribute 'get'
When I run the below code I get dict as the object type
type(all_addr['street address'][0])
But when I run this, I get the error about the float
all_addr['street address'].apply(lambda x: x.get('address_components'))
This is an example of a dictionary that is stored in the column in the DataFrame:
{'address_components': [{'long_name': '871',
'short_name': '871',
'types': ['street_number']},
{'long_name': '8th Avenue', 'short_name': '8th Ave', 'types': ['route']},
{'long_name': 'Manhattan',
'short_name': 'Manhattan',
'types': ['political', 'sublocality', 'sublocality_level_1']},
{'long_name': 'New York',
'short_name': 'New York',
'types': ['locality', 'political']},
{'long_name': 'New York County',
'short_name': 'New York County',
'types': ['administrative_area_level_2', 'political']},
{'long_name': 'New York',
'short_name': 'NY',
'types': ['administrative_area_level_1', 'political']},
{'long_name': 'United States',
'short_name': 'US',
'types': ['country', 'political']},
{'long_name': '10019', 'short_name': '10019', 'types': ['postal_code']},
{'long_name': '5761',
'short_name': '5761',
'types': ['postal_code_suffix']}],
'formatted_address': '871 8th Ave, New York, NY 10019, USA',
'geometry': {'location': {'lat': 40.7637343, 'lng': -73.985334},
'location_type': 'ROOFTOP',
'viewport': {'northeast': {'lat': 40.76508328029149,
'lng': -73.9839850197085},
'southwest': {'lat': 40.7623853197085, 'lng': -73.9866829802915}}},
'place_id': 'ChIJtVPYoldYwokRyDoPj7jHHwk',
'plus_code': {'compound_code': 'Q277 FV New York, NY, USA',
'global_code': '87G8Q277 FV'},
'types': ['street_address']}
Essentially I just want to get the address_components list out of this dictionary so I can then get to some of the data elements held inside that list. I have used similar functions with DataFrames and dictionaries before and have not run into this error.
I am running this in Google Colab and the dictionary data is coming from the Google Maps API.
CodePudding user response:
It might help to show the ouput for
all_addr['street address'].apply(lambda x: type(x))
. Also, .apply()
is pretty slow for just getting values out of a column so pd.json_normalize()
may help in getting just extracting the address_components list for you.