Hi I have a following df:
JavaScript
x
7
1
sentence osm_id place
2
0 #SP486 in località Pianezzo, SENSO UNICO ALTER... 62053.0 ponte
3
1 In provincia di Modena frana sulla strada prov 22165.0 samone
4
2 Provincia di Modena: Pavullo, frana lungo la s 62053.0 ponte
5
3 Provincia di Modena: Pavullo, frana sulla sp 4... 22165.0 #
6
4 Provincia di Modena: Prignano, i lavori su una NaN NaN
7
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:
JavaScript
1
9
1
series = pd.Series(table['place'])
2
3
if series.apply(lambda x: x==x):
4
table['Lat'] = 0
5
table['Lon'] = 0
6
elif series.apply(lambda x: x!=x):
7
table['Lat'] = table.place.apply(lambda x: coord_map[x][1])
8
table['Lon'] = table.place.apply(lambda x: coord_map[x][0])
9
the error that I get:
JavaScript
1
8
1
Traceback (most recent call last):
2
File "c:/Users/user/GeoParsing/main.py", line 136, in <module>
3
if series.apply(lambda x: x==x):
4
File "C:UsersuserGeoParsingvenvlibsite-packagespandascoregeneric.py", line 1442, in __nonzero__
5
raise ValueError(
6
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
7
8
Advertisement
Answer
You can try:
JavaScript
1
3
1
table['Lat'] = table.place.map(lambda x: 0 if (pd.isna(x) or (x == '#')) else coord_map[x][1])
2
table['Lon'] = table.place.map(lambda x: 0 if (pd.isna(x) or (x == '#')) else coord_map[x][0])
3