想實作的是利用經緯度 計算多個臺站和多個移動車的距離,然后在移動車的表格中添加一列用來記錄。臺站資料在一個exel表格中,移動車在一個csv檔案中。
現在的問題是當計算完一個臺站和全部移動車的距離之后便停止計算報錯single positional indexer is out-of-bounds,查了很多方法仍未解決。以及不知道自己這樣添加新列的方法對么,有沒有什么更好的添加新列的方法。還有個問題就是這樣的代碼寫出來后每次執行都會將表格內容復制一遍是為什么呢?
報錯如下
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-16-3a8909f66bba> in <module>
23 lng = table.cell(index_taizhan,13).value#讀取臺站的經度資訊
24 lat = table.cell(index_taizhan,14).value#讀取臺站的緯度資訊
---> 25 x = data_test.iloc[index_test,3]
26 y = data_test.iloc[index_test,4]#分別讀取移動車的經緯度
27 a = int(haversine(lng,lat,x,y))#計算移動車和臺站的距離(單位米)
E:\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
1492 except (KeyError, IndexError, AttributeError):
1493 pass
-> 1494 return self._getitem_tuple(key)
1495 else:
1496 # we by definition only have the 0th axis
E:\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
2141 def _getitem_tuple(self, tup):
2142
-> 2143 self._has_valid_tuple(tup)
2144 try:
2145 return self._getitem_lowerdim(tup)
E:\Anaconda3\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key)
221 raise IndexingError('Too many indexers')
222 try:
--> 223 self._validate_key(k, i)
224 except ValueError:
225 raise ValueError("Location based indexing can only have "
E:\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
2068 return
2069 elif is_integer(key):
-> 2070 self._validate_integer(key, axis)
2071 elif isinstance(key, tuple):
2072 # a tuple should already have been caught by this point
E:\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_integer(self, key, axis)
2137 len_axis = len(self.obj._get_axis(axis))
2138 if key >= len_axis or key < -len_axis:
-> 2139 raise IndexError("single positional indexer is out-of-bounds")
2140
2141 def _getitem_tuple(self, tup):
IndexError: single positional indexer is out-of-bounds
代碼如下
import pandas as pd #資料分析
import numpy as np #科學計算
import xlrd#excel處理
data_taizhan = xlrd.open_workbook("F:/data_mining/taizhan.xlsx")#打開臺站資料表格
table = data_taizhan.sheets()[0]
taizhan_nrows = table.nrows#讀取臺站個數
print("taizhan_nrows=",taizhan_nrows)
data_test = pd.read_csv("F:/data_mining/test.csv",delimiter="\t")#打開移動車資料表格
test_nrows = 0
for key,value in enumerate(open("F:/data_mining/test.csv",'r')):
test_nrows += 1
print("test_nrows=",test_nrows)#較大檔案讀取行數
#計算列數print(len(list(data_test)))
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2): # 定義利用經緯度計算距離的函式
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# 將十進制度數轉化為弧度
lon1, lat1, lon2, lat2 = map(radians,[lon1, lat1, lon2, lat2])
# haversine公式
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # 地球平均半徑,單位為公里
return c * r * 1000
index_taizhan = 1
index_test = 0
while index_taizhan <= taizhan_nrows:
while index_test < test_nrows:#設定回圈,分別計算一個移動車的資料與每個臺站之間的距離
lng = table.cell(index_taizhan,13).value#讀取臺站的經度資訊
lat = table.cell(index_taizhan,14).value#讀取臺站的緯度資訊
x = data_test.iloc[index_test,3]
y = data_test.iloc[index_test,4]#分別讀取移動車的經緯度
a = int(haversine(lng,lat,x,y))#計算移動車和臺站的距離(單位米)
data1 = data_test['Area']
data_test['distance'] = data1
data_test.to_csv("F:/data_mining/test.csv",mode = 'a',index = False)
data_test.iloc[index_test,8] = a
print(a)#輸出距離
index_test = index_test+1
print("00000000000000000000000000")
index_taizhan = index_taizhan + 1
print("1111111111111111111111111111")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/129630.html
