I have a .csv file with GPS data which looks like this:
ID,GPS_LATITUDE,GPS_LONGITUDE
1,35.66727683,139.7591279
2,35.66727683,139.7591279
3,-1,-1
4,35.66750697,139.7589757
5,,139.7589757
The last row has a blank or "null" value. I would like to read the data into a dataframe and set the null value to -1 and also read the data in as type float. With my code the data type is set to string and the null value is not substituted.
How I'm trying to do it (wrong):
data = r'c:\temp\gps.csv'
def conv(val):
if val == np.nan:
return -1
return val
df = pd.read_csv(data,converters={'GPS_LATITUDE':conv,'GPS_LONGITUDE':conv},dtype={'GPS_LATITUDE':np.float64,'GPS_LONGITUDE':np.float64})
Code to test output:
lats = df['GPS_LATITUDE'].tolist()
for l in lats:
print(l,type(l))
df
Output:
35.66727683 <class 'str'>
35.66727683 <class 'str'>
-1 <class 'str'>
35.66750697 <class 'str'>
<class 'str'>
Out[63]:
ID GPS_LATITUDE GPS_LONGITUDE
0 1 35.66727683 139.7591279
1 2 35.66727683 139.7591279
2 3 -1 -1
3 4 35.66750697 139.7589757
4 5 139.7589757