2015-04-23 32 views
0

我正在嘗試使用haversine公式計算兩對lat/long之間的距離。我正在使用一系列的最後兩個函數參數,因爲我正在試圖計算這個數據用於存儲在兩個熊貓列中的多個座標。我收到以下錯誤TypeError: ("'Series' object is not callable", u'occurred at index 0')使用帶有一系列參數的pd.apply會導致TypeError

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from math import radians, cos, sin, asin, sqrt 

origin_lat = 51.507200 
origin_lon = -0.127500 

def haversine(lon1, lat1, lon2, lat2): 
    """ 
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees) 
    """ 
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2]) 

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 
    c = 2 * np.arcsin(np.sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles 
    return c * r 

df['dist_from_org'] = df.apply(haversine(origin_lon, origin_lat, df['ulong'], df['ulat']), axis=1) 

該系列從DF這個樣子的:

+----+---------+----------+ 
| | ulat | ulong | 
+----+---------+----------+ 
| 0 | 52.6333 | 1.30000 | 
| 1 | 51.4667 | -0.35000 | 
| 2 | 51.5084 | -0.12550 | 
| 3 | 51.8833 | 0.56670 | 
| 4 | 51.7667 | -1.38330 | 
| 5 | 55.8667 | -2.10000 | 
| 6 | 55.8667 | -2.10000 | 
| 7 | 52.4667 | -1.91670 | 
| 8 | 51.8833 | 0.90000 | 
| 9 | 53.4083 | -2.14940 | 
| 10 | 53.0167 | -1.73330 | 
| 11 | 51.4667 | -0.35000 | 
| 12 | 51.4667 | -0.35000 | 
| 13 | 52.7167 | -1.36670 | 
| 14 | 51.4667 | -0.35000 | 
| 15 | 52.9667 | -1.16667 | 
| 16 | 51.4667 | -0.35000 | 
| 17 | 51.8833 | 0.56670 | 
| 18 | 51.8833 | 0.56670 | 
| 19 | 51.4833 | 0.08330 | 
| 20 | 52.0833 | 0.58330 | 
| 21 | 52.3000 | -0.70000 | 
| 22 | 51.4000 | -0.05000 | 
| 23 | 51.9333 | -2.10000 | 
| 24 | 51.9000 | -0.43330 | 
| 25 | 53.4809 | -2.23740 | 
| 26 | 51.4853 | -3.18670 | 
| 27 | 51.2000 | -1.48333 | 
| 28 | 51.7779 | -3.21170 | 
| 29 | 51.4667 | -0.35000 | 
| 30 | 51.7167 | -0.28330 | 
| 31 | 52.2000 | 0.11670 | 
| 32 | 52.4167 | -1.55000 | 
| 33 | 56.5000 | -2.96670 | 
| 34 | 51.2167 | -1.05000 | 
| 35 | 51.8964 | -2.07830 | 
+----+---------+----------+ 

不允許我在pd.apply功能,採用了一系列的?如果是這樣的話,我怎樣才能逐行應用一個函數並將輸出分配給一個新列?

+0

檢查您的代碼'df.apply(...',不應該是'pd.apply(...'?PS我對熊貓一無所知 – Pynchia

+0

@Pynchia不,我幾乎積極,它需要爲'df',代碼不顯示,但我的數據幀被稱爲'df' – metersk

+0

什麼是origin_lon,origin_lat?常量還是它們對錶中的每一行都進行更改?順便說一句,它是一個DataFrame,而不是一系列(有多列) – Alexander

回答

1

調用函數時不需要使用apply。只需使用:

df['dist_from_org'] = haversine(origin_lon, origin_lat, df['ulong'], df['ulat']) 

當我運行的代碼(使用標值origin_lon,origin_lat,我有類型錯誤:無法轉換系列這是由分配a = ...

我修改了公式造成的。適用於系列:

a = dlat.divide(2).apply(sin).pow(2) 
    + lat1.apply(cos).multiply(lat2.apply(cos).multiply(dlon.divide(2).apply(sin).pow(2))) 

讓我知道這對你的作品

如果origin_lon和origin_lat是常數(如反對。由於lon2和LAT2是熊貓系列,dlon和DLAT的參數都將被系列對象以及

a = dlat.divide(2).apply(sin).pow(2) + cos(lat1) * lat2.apply(cos).multiply(dlon.divide(2).apply(sin).pow(2)) 

:d爲一系列),然後使用這個公式。然後您需要在該系列上使用apply將該功能應用於列表中的每個元素。

+0

仍然收到錯誤TypeError:(「'系列'對象不可調用」, u'發生在索引0')' – metersk

+0

try >>> haversine(origin_lon,origin_lat,df ['ulong'],df ['ulat'])返回什麼? – Alexander

+0

仍然返回相同的錯誤 – metersk

相關問題