python pandas数据类型错误浮点数是必需的

时间:2014-11-21 21:47:26

标签: python types pandas

我正在尝试从csv文件读取数据并从坐标计算方位。但是我得到错误'需要浮点数'。如果我自己运行函数(不使用循环)只有两个坐标,函数本身就可以工作。所以我认为这个问题与最后一个'for'声明有关。我想知道是否有人知道我应该在哪里以及如何设置float数据类型?谢谢。

from math import *
import pandas as p
import numpy as np

bearingdata = 'xxxxxx.csv'
data = p.read_csv(bearingdata)

lat = [float(i) for i in data.Lat]
lon = [float(j) for j in data.Lon]

lat1 = lat[0: (len(lat) -2)]
lon1 = lon[0: (len(lon) -2)]
lat2 = lat[1: (len(lat) -1)]
lon2 = lon[1: (len(lon) -1)]

def bearing(lon_1, lat_1, lon_2, lat_2):

    # convert decimal degrees to radians 
        lon_1, lat_1, lon_2, lat_2 = map(radians, [lon_1, lat_1, lon_2, lat_2])

    #now calcuate bearing   

    Bearing = atan2(cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1),sin(lon2-lon1)*cos(lat2))
    Bearing = degrees(Bearing)
    Bearing = (Bearing + 360) % 360

    return Bearing   

count = 0
for x in lat1:
    print str(count) + "\n"

    angle = bearing(lon1[count], lat1[count], lon2[count], lat2[count])
    print "the bearing between " + str(lat1[count]) + "," + str(lon1[count]) + " and " + str(lat2[count]) + "," + str(lon2[count]) + " is: " + str(angle) + "degrees \n"   
    count = count + 1

*追溯*

Traceback (most recent call last):<br>
File "bearing.py", line 34, in <module><br>
angle = bearing(lon1[count], lat1[count], lon2[count], lat2[count])<br>
File "bearing.py", line 23, in bearing<br>
Bearing = atan2(cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1),sin(lon2-lon1)*cos(lat2))<br>
TypeError: a float is required

*原始数据如下*

Lat(列名)
42.xxxxxx
......许多行

Lon(列名)
78.xxxx
......很多行

2 个答案:

答案 0 :(得分:0)

在函数中为数学运算添加小数:

Bearing = (Bearing + 360) % 360.0

答案 1 :(得分:0)

你几乎不需要在熊猫中循环。尝试这样的事情:

生成一些假坐标

import pandas
import numpy as np

N = 10
np.random.seed(0)
degree_data = pandas.DataFrame({
    'lat': np.random.uniform(low=-90, high=90, size=N),
    'lon': np.random.uniform(low=-180, high=180, size=N),
})

degree_data看起来像这样:

         lat         lon
0   8.786431  105.021014
1  38.734086   10.402171
2  18.497408   24.496042
3   8.078973  153.214790
4 -13.742136 -154.427019
5  26.260940 -148.633452
6 -11.234302 -172.721377
7  70.519140  119.743144
8  83.459297  100.136430
9 -20.980527  133.204373

转换为弧度并将所有内容向上移动1行,以便我们可以向前看

radian_data = np.round(np.radians(degree_data), 2)
radian_data = radian_data.join(radian_data.shift(-1), lsuffix='1', rsuffix='2')
print(radian_data)

所以现在radian_data是:

   lat1  lon1  lat2  lon2
0  0.15  1.83  0.68  0.18
1  0.68  0.18  0.32  0.43
2  0.32  0.43  0.14  2.67
3  0.14  2.67 -0.24 -2.70
4 -0.24 -2.70  0.46 -2.59
5  0.46 -2.59 -0.20 -3.01
6 -0.20 -3.01  1.23  2.09
7  1.23  2.09  1.46  1.75
8  1.46  1.75 -0.37  2.32
9 -0.37  2.32   NaN   NaN

定义从数据框

获取行的方位函数
def bearing(row):
    x = np.cos(row.lat1)*np.sin(row.lat2) - \
        np.sin(row.lat1)*np.cos(row.lat2)*np.cos(row.lon2-row.lon1)
    y = np.sin(row.lon2-row.lon1)*np.cos(row.lat2)
    Bearing = np.degrees(np.arctan2(x, y))
    return (Bearing + 360) % 360

apply对每一行起作用,保存在原始数据框

degree_data['bearing'] = radian_data.apply(bearing, axis=1)
# so now we have 
         lat         lon     bearing
0   8.786431  105.021014  140.855914
1  38.734086   10.402171  305.134809
2  18.497408   24.496042   22.751374
3   8.078973  153.214790  337.513363
4 -13.742136 -154.427019   81.301311
5  26.260940 -148.633452  235.214299
6 -11.234302 -172.721377  108.063240
7  70.519140  119.743144   98.957144
8  83.459297  100.136430  301.528278
9 -20.980527  133.204373         NaN