how do i extract individual returns from maps?

You can use zip to unpack the tuple inside the geolocation column:

def add_geolocation(df, country_column):
    df["geolocation"] =
    df['lat'], df['long'] = zip(*df['geolocation'])
    return df

Edit: what does zip(*df['geolocation']) do? This is a combination of two different concepts: the star (*) operator and the zip function.

The * operator unpacks the collection into positional arguments. The following two calls are equivalent:

def f(a, b):
    return a + b

f(1, 2) # return 3

lst = [1,2]
f(*lst) # return 3

The zip function returns elements from the input collections pair-wise:

zip([1,2], ['A', 'B'], ['One', 'Two']) # return (1, 'A', 'One'), (2, 'B', 'Two')

We combine the two here to split the first elements of the geolocation tuples into a separate collection, and the second elements into another collection:


zip(*df['geolocation']) == zip((1,2), (3,4), (5,6)) == [(1,3,5), (2,4,6)]
# The first is a collection of latitudes, second is longitudes

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top