Reputation: 11
I'm trying to split an address column in a data frame, using pandas, to add to a dictionary for geocoding. I've done it before but not using pandas and jupyterlab. there Address column looks like 123 street, city,state,zipcode
I've created a dictionary #create Dictionary to hold address data
meeting_address = alcoholics_anonymous['Address'].tolist()
and the outcome I want would be written #split address data in dictionary by Street, city, state, and zipcode
meeting_address =[dict(zip(['street','city','state','zip'], address.split(',')[:-1]))for address in meeting_address]
However with pandas in Jupyter notebook the address.split doesn't work any help?
meeting_address =[dict(zip(['street','city','state','zip'], address.split(',')[:-1]))for address in meeting_address]
what is the best way to split the data in the list and add to the dictionary?
Upvotes: 1
Views: 165
Reputation: 4105
I think you were very close.
Here is my answer:
# Sample addresses inserted into a list
meeting_address = ['22222 Acoma St,Proston,QLD,4613',
'534 Schoenborn St,Hamel,WA,6215',
'69206 Jackson Ave,Talmalmo,NSW,2640',
'808 Glen Cove Ave,Lane Cove,NSW,1595',
'373 Lafayette St,Cartmeticup,WA,6316',
'87 Sylvan Ave,Nyamup,WA,6258']
# The keys for the dictionary
keys = ['street','city','state','zip']
# Convert the meeting_addresses into a dictionary using keys
address_dicts = [dict(zip(keys, address.split(','))) for address in meeting_address]
# Print the address dicts in the list
for ad in address_dicts:
print(ad)
OUTPUT:
{'street': '22222 Acoma St', 'city': 'Proston', 'state': 'QLD', 'zip': '4613'}
{'street': '534 Schoenborn St', 'city': 'Hamel', 'state': 'WA', 'zip': '6215'}
{'street': '69206 Jackson Ave', 'city': 'Talmalmo', 'state': 'NSW', 'zip': '2640'}
{'street': '808 Glen Cove Ave', 'city': 'Lane Cove', 'state': 'NSW', 'zip': '1595'}
{'street': '373 Lafayette St', 'city': 'Cartmeticup', 'state': 'WA', 'zip': '6316'}
{'street': '87 Sylvan Ave', 'city': 'Nyamup', 'state': 'WA', 'zip': '6258'}
Upvotes: 1