Python Forum

Full Version: IndexError: index 0 is out of bounds for axis 0 with size 0
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi friends, need your help. I have this problem with an Excel file wherein it works with my first file but second Excel has an error. Thanks!

Error:
Traceback (most recent call last): File "C:\DATA\python\app.py", line 442, in <module> main() File "C:\DATA\python\app.py", line 366, in main shortestSen = shortestSentence(baseDF) File "C:\DATA\python\app.py", line 258, in shortestSentence index = index[[0]] IndexError: index 0 is out of bounds for axis 0 with size 0
def shortestSentence(baseDF):
    addList = ["SENTENCE", "Sentence", "sentence"]
    shortValsList = []
    indexList = []
    d = {}
    for c in baseDF:
        for i in addList:
            if i[:4] in c:
                length = baseDF[c].astype(str).map(len)
                shortVal = baseDF.loc[length.idxmin(), str(c)]
                shortValsList.append(shortVal)
                index = np.flatnonzero(baseDF[c] == shortVal)
                index = index[[0]]
                indexList.append(index)
                d[shortVal] = index
            else:
                pass
    shortest = max(shortValsList, key = len)
    shortestInd = d[shortest]
    shortestSen = baseDF.iloc[shortestInd]
    return shortestSen
Maybe there's no data in that cell? If you can post a minimally reproducible example, you might get more answers..
(Feb-27-2021, 12:53 AM)nealc Wrote: [ -> ]Maybe there's no data in that cell? If you can post a minimally reproducible example, you might get more answers..

my Excel data is just any normal text with headers such as NAME1, NAME2, SENT1, SENT2...

and the contents are just words less than 50 characters.

I'm using pandas and numpy.