Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using pandas, index error
I am trying to round numbers, if their decimal numbers end with (125, 375, 625, 875), I replace them with (25, 25, 75, 75). My code is:

import pandas as pd  

inpExcelFile = 'Lat Lon Coordinates and soil.csv'  #lat, lon
gridCells = pd.read_csv(inpExcelFile)
#inpExcelFile can be downloaded from ""

df = pd.DataFrame(columns=['lat_ext', 'lon_ext', 'lat_close', 'lon_close'])

for i in range(0, len(gridCells.index)):  

    if str([i, 'lat_ext'])== str('125'):[i, 'lat_close'] =  float((str([i, 'lat'])[:3]) + str('25'))
    if str([i, 'lat_ext'])== str('375'):[i, 'lat_close'] = float((str([i, 'lat'])[:3]) + str('25'))
    if str([i, 'lat_ext'])== str('625'):[i, 'lat_close'] = float((str([i, 'lat'])[:3]) + str('75'))
    if str([i, 'lat_ext'])== str('875'):[i, 'lat_close'] = float((str([i, 'lat'])[:3]) + str('75'))

for j in range(0, len(gridCells.index)):  

    if str([j, 'lon_ext'])== str('125'):[j, 'lon_close'] =  float((str([j, 'lat'])[:3]) + str('25'))
    if str([j, 'lon_ext'])== str('375'):[j, 'lon_close'] = float((str([j, 'lat'])[:3]) + str('25'))
    if str([j, 'lon_ext'])== str('625'):[j, 'lon_close'] = float((str([j, 'lat'])[:3]) + str('75'))
    if str([j, 'lon_ext'])== str('875'):[j, 'lon_close'] = float((str([j, 'lat'])[:3]) + str('75'))

df.to_excel('cikti' + '.xlsx')
I am getting this error:
File "<ipython-input-1-eff718b7025b>", line 1, in <module> runfile('C:/Users/fyunu/OneDrive/Masaüstü/code rubbish/', wdir='C:/Users/fyunu/OneDrive/Masaüstü/code rubbish') File "C:\Users\fyunu\Anaconda3\lib\site-packages\spyder\utils\site\", line 705, in runfile execfile(filename, namespace) File "C:\Users\fyunu\Anaconda3\lib\site-packages\spyder\utils\site\", line 102, in execfile exec(compile(, filename, 'exec'), namespace) File "C:/Users/fyunu/OneDrive/Masaüstü/code rubbish/", line 20, in <module> if str([i, 'lat_ext'])== str('125'): File "C:\Users\fyunu\Anaconda3\lib\site-packages\pandas\core\", line 2141, in __getitem__ key = self._convert_key(key) File "C:\Users\fyunu\Anaconda3\lib\site-packages\pandas\core\", line 2227, in _convert_key raise ValueError("At based indexing on an non-integer " ValueError: At based indexing on an non-integer index can only have non-integer indexers
How can I solve that problem?
buran wrote Aug-01-2018, 07:12 AM:
Please, post the entire traceback that you get. We need to see that whole thing. Do not just give us the last line.
Take a time to read What to include in a post
Disregarding the fact that this is misuse of pandas API, your df is empty - so applying cannot yield any result
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to speed up work with pandas index? AlekseyPython 1 155 Oct-16-2019, 02:06 PM
Last Post: AlekseyPython
  pandas DataReader error on all data sources glidecode 5 6,467 Sep-25-2019, 02:10 PM
Last Post: perfringo
  Key error when using adodbapi, azure and pandas mazamus 2 584 Sep-11-2019, 06:54 AM
Last Post: TomKom
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 344 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  pandas: can we look for the index of a string paul18fr 2 370 Jul-31-2019, 08:25 AM
Last Post: paul18fr
  Unable to identify Tuple index error appmkl 4 471 Jun-28-2019, 10:12 AM
Last Post: appmkl
  pandas install error loren41 5 614 May-20-2019, 06:42 PM
Last Post: loren41
  Can you help me with this error? IndexError: invalid index of a 0-dim tensor. DerBerliner 1 944 Feb-28-2019, 05:47 PM
Last Post: Larz60+
  utf-8 error with pandas read_csv logues 0 1,000 Oct-23-2018, 05:25 PM
Last Post: logues
  How to get first and last row index of each unique names in pandas dataframe SriRajesh 1 1,164 Oct-13-2018, 07:04 AM
Last Post: perfringo

Forum Jump:

Users browsing this thread: 1 Guest(s)