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!

3
  • what is going on in the original df that you are getting an empty index being returned? Commented Oct 26, 2022 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 Commented Oct 26, 2022 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. Commented Oct 27, 2022 at 13:44

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.