Trouble importing Excel fields into Python via Pandas - index out of bounds error

Viewed 669

I'm not sure what happened, but my code has worked today, however not it won't. I have an Excel spreadsheet of projects I want to individually import and put into lists. However, I'm getting a "IndexError: index 8 is out of bounds for axis 0 with size 8" error and Google searches have not resolved this for me. Any help is appreciated. I have the following fields in my Excel sheet: id, funding_end, keywords, pi, summaryurl, htmlabstract, abstract, project_num, title. Not sure what I'm missing...

import pandas as pd

dataset = pd.read_excel('new_ahrq_projects_current.xlsx',encoding="ISO-8859-1")
df = pd.DataFrame(dataset)
cols = [0,1,2,3,4,5,6,7,8]
df = df[df.columns[cols]]

tt = df['funding_end'] = df['funding_end'].astype(str)
tt = df.funding_end.tolist()
for t in tt:
   allenddates.append(t)

bb = df['keywords'] = df['keywords'].astype(str)
bb = df.keywords.tolist()
for b in bb:
   allkeywords.append(b)

uu = df['pi'] = df['pi'].astype(str)
uu = df.pi.tolist()
for u in uu:
   allpis.append(u)

vv = df['summaryurl'] = df['summaryurl'].astype(str)
vv = df.summaryurl.tolist()
for v in vv:
   allsummaryurls.append(v)

ww = df['htmlabstract'] = df['htmlabstract'].astype(str)
ww = df.htmlabstract.tolist()
for w in ww:
   allhtmlabstracts.append(w) 

xx = df['abstract'] = df['abstract'].astype(str)
xx = df.abstract.tolist()
for x in xx:
   allabstracts.append(x) 

yy = df['project_num'] = df['project_num'].astype(str)
yy = df.project_num.tolist()
for y in yy:
   allprojectnums.append(y)    

zz = df['title'] = df['title'].astype(str)
zz = df.title.tolist()

for z in zz:
   alltitles.append(z) 
3 Answers

"IndexError: index 8 is out of bounds for axis 0 with size 8"

cols = [0,1,2,3,4,5,6,7,8]

should be cols = [0,1,2,3,4,5,6,7].

I think you have 8 columns but your col has 9 col index.

I agree with @Bill CX that it sounds like you're trying to access a column that doesn't exist. Although I cannot reproduce your error, I have some ideas that may help you move forward.

First, double check the shape of your data frame:

import pandas as pd

dataset = pd.read_excel('new_ahrq_projects_current.xlsx',encoding="ISO-8859-1")
df = pd.DataFrame(dataset)
print(df.shape) # print shape of data read in to python

The output should be

(X, 9) # "X" is the number of rows

If the data frame has 8 columns, then the df.shape will be (X, 8). This could be why your are getting the error.

Another check for you is to print out the first few rows of your data frame.

print(df.head)

This will let you double-check to see if you have read in the data in the correct form. I'm not sure, but it might be possible that your .xlsx file has 9 columns, but pandas is reading in only 8 of them.

IndexError: index out of bounds means you're trying to insert or access something which is beyond its limit or range.

Every time, when you load either of these files such as an test.xlx, test.csv or test.xlsx file using Pandas such as:

data_set = pd.read_excel('file_example_XLS_10.xls', encoding="ISO-8859-1")

It would be better for everyone to find the length of columns of a DataFrame that will help you move forward when working with large Data_Sets. e.g.

import pandas as pd

data_set = pd.read_excel('file_example_XLS_10.xls', encoding="ISO-8859-1") 
data_frames = pd.DataFrame(data_set)

print("Length of Columns:", len(data_frames.columns))

This will give you the exact number of columns of an Excel Spread-Sheet. Then you can specify the Data Frames Accordingly:

Length of Columns: 8

cols = [0, 1, 2, 3, 4, 5, 6, 7]

Related