I have a gps parameters data-frame with different types of data (integers, objects, floats, and strings) as follows:
ID time speed pdop longitude latitude
0 356363050613605 2017-02-20T17:04:40 210 3.051 8.81640 44.75045
1 356363050637620 2017-02-21T13:44:38 172 60.00 9.41286 45.48268
Then by using the geopy package i'have parsed the latitude and longitudes co-ordinates into geographical locations using the following code
import geopy
from geopy.geocoders import Nominatim
geolocator = Nominatim()
def f(row):
loc = str(row['latitude']) + ',' +str(row['longitude'])
locations = geolocator.reverse(loc)
return location
df['address'] = df.apply(lambda row: f(row), axis=1)
which returns an address column to the data-frame with the full address and the co-ordinates as a string.
ID time speed pdop longitude latitude address
0 356363050613605 2017-02-20T17:04:40 210 3.051 8.81640 44.75045 (Viale del Lavoro, Zona Industriale C.I.P.I.A.N., Novi Ligure, AL, PIE, 15067, Italia), (44.7524715, 8.8122644)
1 356363050637620 2017-02-21T13:44:38 172 60.00 9.41286 45.48268 (Strada Provinciale 14 Rivoltana, Liscate, MI, LOM, 20066, Italia), (45.4821247, 9.4118136)
The problem now that I'm trying to export the data-frame to a csv-file with "df.to_csv('output.csv')" but it keeps returning the following error:
TypeError: str returned non-string (type NoneType)
I have even tried the following code which functions only when specifying to return the address column as follows:
with open('outpu.csvt', 'w') as file:
a = csv.writer(file)
data = df['address']
a.writerows(data)
While when trying to export the whole data-frame it returns an empty file. Any help how to export the whole data-frame to a csv file??
I'm not familiar with geolocator at all, but it appears that your function is returning a list (or tuple) of tuples. If you instead returned something like:
return ','.join(location[0])
That should return a single string to your address
column which should be easily writable to csv.