'How to resolve TypeError: zip argument #1 must support iteration?

Hope you are doing great!

I am currently working on task where I need to map lat and long using folium. In my below code I am trying to loop through the data frame on specific columns to add relevant colors and markers. But it gives me an error "TypeError: zip argument #1 must support iteration"

Here is the code;

# Loop through frames and add relevant colours, markers etc.

incidents = plugins.MarkerCluster().add_to(city_of_london_map)

for dfx, iconx, icon_colorx in zip(dfs, icons, icon_colors):
    for lat, lng, label, month in zip(dfx['Latitude'], dfx['Longitude'], dfx['Crime_type'], dfx['Month']):
        incidents.add_child(
            folium.Marker(
                [lat, lng],
                icon = folium.Icon(color=icon_colorx,icon=iconx, prefix='fa'),
                popup = folium.Popup(folium.IFrame(f'''<html style="text-align:center; font-family:verdana; font-size:80%;width:100%; height:100%;"><strong>Month:</strong><br>{month}<br><strong>Crime Type:</strong><br>{label}</html>'''),
                                     min_width=150,max_width=150)
            )
            
        )
        
# Tidy up the layers

incidents.layer_name = 'Markers'

below is the data types;

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 30894 entries, 0 to 30893
Data columns (total 12 columns):
Crime_ID                 26497 non-null object
Month                    30894 non-null object
Reported_by              30894 non-null object
Falls_within             30894 non-null object
Longitude                30894 non-null float64
Latitude                 30894 non-null float64
Location                 30894 non-null object
LSOA_name                28580 non-null object
Crime_type               30894 non-null object
Last_outcome_category    26497 non-null object
Months                   30894 non-null int32
years                    30894 non-null int32
dtypes: float64(2), int32(2), object(8)
memory usage: 2.6+ MB

Here is the error;

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-166-d9b90b67b511> in <module>
      4 
      5 for dfx, iconx, icon_colorx in zip(dfs, icons, icon_colors):
----> 6     for lat, lng, label, month in zip(dfx['Latitude'], dfx['Longitude'], dfx['Crime_type'], dfx['Month']):
      7         incidents.add_child(
      8             folium.Marker(

TypeError: zip argument #1 must support iteration

May I please ask for your help/support to solve this error?



Solution 1:[1]

zip works on iterables, i.e. takes iterables as parameters, and returns an iterator. See: https://docs.python.org/3.3/library/functions.html#zip

Your dfx['Latitude'], dfx['Longitude'], dfx['Crime_type'], dfx['Month'] seem to be values and not iterables.

Can you try this instead:

for dfx, iconx, icon_colorx in zip(dfs, icons, icon_colors):
    lat = dfx['Latitude']
    lng = dfx['Longitude']
    label = dfx['Crime_type']
    month = dfx['Month']
    # further code here..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iR0ckY