Skip to content
Advertisement

Check if the dtype:object is NaN

Hi I have a following df:

                                           sentence   osm_id   place
0  #SP486 in località Pianezzo, SENSO UNICO ALTER...  62053.0   ponte
1  In provincia di Modena frana sulla strada prov...  22165.0  samone
2  Provincia di Modena: Pavullo, frana lungo la s...  62053.0   ponte
3  Provincia di Modena: Pavullo, frana sulla sp 4...  22165.0       #
4  Provincia di Modena: Prignano, i lavori su una...      NaN     NaN

I would like to to add the latitude and longitude columns after the city name, while where there are NaN and # value I would like to leave new cells empty or with 0 value.

what I tried:

series = pd.Series(table['place'])

if series.apply(lambda x: x==x):
    table['Lat'] = 0
    table['Lon'] = 0
elif series.apply(lambda x: x!=x): 
    table['Lat'] = table.place.apply(lambda x: coord_map[x][1])
    table['Lon'] = table.place.apply(lambda x: coord_map[x][0])

the error that I get:

Traceback (most recent call last):
  File "c:/Users/user/GeoParsing/main.py", line 136, in <module>
    if series.apply(lambda x: x==x):
  File "C:UsersuserGeoParsingvenvlibsite-packagespandascoregeneric.py", line 1442, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Advertisement

Answer

You can try:

table['Lat'] = table.place.map(lambda x: 0 if (pd.isna(x) or (x == '#')) else coord_map[x][1])
table['Lon'] = table.place.map(lambda x: 0 if (pd.isna(x) or (x == '#')) else coord_map[x][0])
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement