How to iterate through dataframe to add traces into go.scattermapbox?


I have a dataframe which consists of lat, lon, location. Since I have other big data sets with another kind of data I will use plotly go to add different data. Many files have always the same structure and I often need to copy paste, is there a way to add traces in a go.scattermapbox with a loop?

This is the method I use right now where I have to copy paste it for every entry.

fig = go.Figure(go.Scattermapbox(
    mode = "markers+lines",
    lon = [lon],
    lat = [lat],
    marker = {'size': 10}))

My data:

First consider if you really need to iterate over rows in a DataFrame. Iterating through pandas dataFrame objects is generally slow. Iteration beats the whole purpose of using DataFrame. It is an anti-pattern and is something you should only do when you have exhausted every other option. It is better look for a List Comprehensions , vectorized solution or DataFrame.apply() method for iterate through DataFrame.

Pandas DataFrame loop using list comprehension

result = [(x, y,z) for x, y,z in zip(df['Name'], df['Promoted'],df['Grade'])]

Pandas DataFrame loop using DataFrame.apply()

result = df.apply(lambda row: row["Name"] + " , " + str(row["TotalMarks"]) + " , " + row["Grade"], axis = 1)