Home > other >  [small white ask] single positional indexer is the out - of - bounds related issues
[small white ask] single positional indexer is the out - of - bounds related issues

Time:10-17

Want to achieve is the latitude and longitude is used to calculate multiple stations and multiple mobile car in the distance, and then add a column in a table in the mobile car used to record, station data in a exel form, mobile car in a CSV file,
Finish the question now is when calculating a stations and all mobile car distance after stopping the calculation error single positional indexer is the out - of - bounds, checked many ways is still not solved, and the method of adding new columns don't know oneself to yao, what better way to add a new column, there is a problem is that the code written after each will copy the form content is why?


error below
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
IndexError Traceback (the most recent call last)

23 LNG=table. The cell (index_taizhan, 13). Value# read station longitude information
Lat 24=table. The cell (index_taizhan, 14). Value# read latitude in the station information
- & gt; 25 x=data_test. Iloc [index_test, 3)
26 y=data_test. Iloc [index_test, 4] # read the mobile car of latitude and longitude respectively
27 a=int (haversine (LNG, lat, x, y)) # to calculate the distance the mobile car and stations (in meters)

E: \ Anaconda3 \ lib \ site - packages, pandas, core, indexing, py in the __getitem__ (self key)
1492 the except (KeyError IndexError AttributeError) :
Pass 1493
-> 1494 the return of the self. _getitem_tuple (key)
1495 else:
1496 # we by definition only have the 0 th axis

E: \ Anaconda3 \ lib \ site - packages, pandas, core, indexing, py in _getitem_tuple (self, tup)
2141 def _getitem_tuple (self, tup) :
2142
- & gt; 2143 self. _has_valid_tuple (tup)
2144 try:
2145 the return of the self. _getitem_lowerdim (tup)

E: \ Anaconda3 \ lib \ site - packages, pandas, core, indexing, py in _has_valid_tuple (self key)
221 the -raise IndexingError (' Too many indexers')
222 try:
--> 223 self. _validate_key (k, I)
224 the except ValueError:
225 the -raise ValueError (" Location -based indexing can only have "

E: \ Anaconda3 \ lib \ site - packages, pandas, core, indexing, py in _validate_key (self, key, axis)
The return of 2068
2069 elif is_integer (key) :
-> 2070 self. _validate_integer (key, axis)
2071 elif isinstance (key, a tuple) :
2072 # a tuple should already have had 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 & gt;=len_axis or key & lt; - len_axis:
-> 2139 raise IndexError (" single positional indexer is the out - of - bounds ")
2140
2141 def _getitem_tuple (self, tup) :

IndexError: single positional indexer is the out - of - bounds
code below

The import pandas as pd # data analysis
The import numpy as np # scientific computing
The import xlrd# excel processing

Data_taizhan=XLRD. Open_workbook (" F:/data_mining taizhan. XLSX ") # open station data table
Table=data_taizhan. Sheets () [0]
Taizhan_nrows=table. Nrows# read station number
Print (" taizhan_nrows=", taizhan_nrows)

Data_test=pd. Read_csv (" F:/data_mining/test CSV ", delimiter="\ t") # open mobile data table
Test_nrows=0
For the key and the value in enumerate (open (" F:/data_mining/test CSV ", 'r')) :
Test_nrows +=1
Print (" test_nrows=", test_nrows) # larger files rows read

# calculated the number of columns print (len (list (data_test)))
The from math import radians, cosine, sine, asin, SQRT
Def haversine (lon1, lat1, lon2, lat2) : # define the function of the distance calculated using the latitude and longitude
"" "
Calculate the great circle short between two points
On the earth (specified in decimal degrees)
"" "
# convert decimal degrees to radians
Lon1, lat1, lon2, lat2=map (radians, [, lat1, lon1 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 * asin (SQRT (a))
R=6371 # average radius of the earth, the unit is km
Return the c * r * 1000

Index_taizhan=1
Index_test=0

While index_taizhan & lt;=taizhan_nrows:
While index_test & lt; Test_nrows: # set loop, a mobile data are calculated respectively and the distance between each station
LNG=table. The cell (index_taizhan, 13). Value# read station longitude information
Lat.=the table cell (index_taizhan, 14). Value# read latitude in the station information
X=data_test. Iloc [index_test, 3)
Y=data_test. Iloc [index_test, 4] # read the mobile car of latitude and longitude respectively
A=int (haversine (LNG, lat, x, y)) # to calculate the distance the mobile car and stations (in meters)

Data1=data_test [' Area ']
Data_test [' short ']=data1
Data_test. To_csv (" F:/data_mining/test CSV ", mode='a', the index=False)
Data_test. Iloc [index_test, 8]=a
# print (a) output distance
Index_test=index_test + 1
Print (" 00000000000000000000000000 ")
Index_taizhan=index_taizhan + 1
Print (" 1111111111111111111111111111 ")
  • Related