2

I have a dataframe like this:

                    0           1           2           3           4
19238V105         NaN         NaN         NaN         NaN         NaN   
91731X102         NaN         NaN         NaN   2450900.0    996600.0   
97X1              NaN         NaN         NaN         NaN         NaN   

I would like to drop all of the rows where: len(index) != 9. So the result would be:

                    0           1           2           3           4
19238V105         NaN         NaN         NaN         NaN         NaN   
91731X102         NaN         NaN         NaN   2450900.0    996600.0   

EDIT

I wrote this code:

for index, row in df.iterrows():
    if len(index) != 9:
        df = df.drop(index)

Is there a better way? Also, I'm not entirely sure why both the index, row are required and not just index. Thanks

1 Answer 1

2

Try this:

df[df.index.str.len() >= 9]
Sign up to request clarification or add additional context in comments.

1 Comment

I modified this to == 9, but yes, this is what I was looking for. Thank you

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.