Python Forum
Using pandas, index error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using pandas, index error
#1
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 "http://www.evernote.com/l/AWMD9Le15ytJmokhnUF8r84vtXVIsj2_lBQ/"

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

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

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

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

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

df.to_excel('cikti' + '.xlsx')
I am getting this error:
Error:
File "<ipython-input-1-eff718b7025b>", line 1, in <module> runfile('C:/Users/fyunu/OneDrive/Masaüstü/code rubbish/untitled3.py', wdir='C:/Users/fyunu/OneDrive/Masaüstü/code rubbish') File "C:\Users\fyunu\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace) File "C:\Users\fyunu\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/fyunu/OneDrive/Masaüstü/code rubbish/untitled3.py", line 20, in <module> if str(df.at[i, 'lat_ext'])== str('125'): File "C:\Users\fyunu\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2141, in __getitem__ key = self._convert_key(key) File "C:\Users\fyunu\Anaconda3\lib\site-packages\pandas\core\indexing.py", 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?
Reply
#2
Disregarding the fact that this is misuse of pandas API, your df is empty - so applying df.at 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Getting Index Error - list index out of range krishna 2 855 Jan-09-2021, 08:29 AM
Last Post: buran
  strange error from pandas dataframe djf123 1 1,980 Jul-27-2020, 05:25 AM
Last Post: scidam
  error bars with dataframe and pandas Hucky 4 1,758 Apr-27-2020, 02:02 AM
Last Post: Hucky
  pip install pandas ERROR pythondudu 6 13,759 Mar-26-2020, 07:49 PM
Last Post: snippsat
  pandas error Scott 2 3,581 Feb-05-2020, 07:22 PM
Last Post: Scott
  Getting Index Error - list index out of range RahulSingh 2 3,130 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 1,157 Jan-28-2020, 09:30 AM
Last Post: axelle
  How to speed up work with pandas index? AlekseyPython 1 963 Oct-16-2019, 02:06 PM
Last Post: AlekseyPython
  pandas DataReader error on all data sources glidecode 5 13,616 Sep-25-2019, 02:10 PM
Last Post: perfringo
  Key error when using adodbapi, azure and pandas mazamus 2 2,127 Sep-11-2019, 06:54 AM
Last Post: TomKom

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020