0

New to programming so forgive me if I say something erroneous. Im currently working on a small project which plots volcanoes onto a map using folium. The dataset im using can be found here: https://www.ngdc.noaa.gov/nndc/struts/form?t=102557&s=50&d=50 I have saved this as a CSV file to read into my program.

when I run my program, i am getting the following error:

...line 31, in <module>
folium.Marker(location=[lat, lon], popup=name, icon=folium.Icon(color=colour(elev), icon='cloud')).add_to(map)...
ValueError: Location values cannot contain NaNs, got:
[nan, nan]

Am i right in thinking this is because the program is not reading the [lat, lon] values as numbers on line 31? I was initially getting this error on line 16 also, but i have since changed [lat, lon] to [latmean, lonmean] which seems to have rectified the problem, although this suggests the program is reading the numbers in order to apply an average to them in the first place. Either way, im confused as to whats causing the problem. any help would be much appreciated!

my code is as follows:

import pandas as pd
import folium

df = pd.read_csv('data.csv')

latmean = df['Latitude'].mean()
lonmean = df['Longitude'].mean()

map = folium.Map(location=[latmean, lonmean], zoom_start=7, tiles='Stamen 
Terrain')

def colour(elev):
    if elev in range (0, 1000):
        col = 'blue'
    elif elev in range (1001, 1999):
        col = 'green'
    elif elev in range (2000, 2999):
        col = 'orange'
    else:
        col = 'red'
    return col

for lat, lon, name, elev in zip(df['Latitude'], df['Longitude'], df['Name'], 
df['Elevation']):
    folium.Marker(location=[lat, lon], popup=name, icon=folium.Icon(color=colour(elev), icon='cloud')).add_to(map)

print(map.save('test.html'))
Charlie Miller
  • 41
  • 1
  • 1
  • 4

2 Answers2

0

You are correct. Lat and lon are returning non-numeric values or "Not a numbers" (NaNs). Latmean and lonmean are functional because .mean() is performing type coercion to do the math.

You can perform type conversion by calling pd.to_numeric() on each of your dataframe objects before iterating:

df['Latitude'] = pd.to_numeric(df['Latitude'])

http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_numeric.html

Cody DIlls
  • 31
  • 1
0

It seems some of the latitude and longitude coordinates from your data are returning a numpy.nan value. You could avoid this by implementing try-except to check what values are being passed to the location argument in Marker().

multigoodverse
  • 7,638
  • 19
  • 64
  • 106