0

I need the row number of a matching cell that is empty: Int64Index([], dtype='int64'). I am receiving an index 0 is out of bounds for axis 0 with size 0 exception.

Print indices of matching elements:

if len(item) < 9:
    print(item)
    print(df[df['column name'] == item].index)

Output:

nan
1234
abc
Int64Index([], dtype='int64')
Int64Index([209], dtype='int64')
Int64Index([325], dtype='int64')

Print indice number of matching cells:

print(df[df['column name'] == item].index[0])

Output, empty indice throws exception:

index 0 is out of bounds for axis 0 with size 0

Output without blank cell:

208
324

Thanks!

Captain Caveman
  • 1,448
  • 1
  • 11
  • 24
  • what is going on in the original df that you are getting an empty index being returned? – scotscotmcc Oct 26 '22 at 21:34
  • It looks like when you do `df[df['column name'] == nan]`, that filtering is returning an empty dataframe. That is, nothing in the original dataframe has nan as the value there. Then you are trying to print the a row from an empty dataframe, there is nothing to print. You probably want to 1) check if you should be getting nothing from the filter, and/or 2) add a `try:...except:...` for when the filter returns nothing at all – scotscotmcc Oct 26 '22 at 21:37
  • The nan is an empty cell. That is why the index object contains an empty []. I need the index[0] (row number) of that empty cell. – Captain Caveman Oct 27 '22 at 13:44

1 Answers1

0

Get row number(s) of empty cells in column:

empty_cells  = df[df[column].isna()]

Get row numbers of matching cells, whether empty or not. Credit to this Q&A.

all_matches = df[df[column].astype(str).map(len) != 9]
Captain Caveman
  • 1,448
  • 1
  • 11
  • 24