0

I am trying to delete every rows in my dataframe which is having morethan 5 number of NaN values against my 'Station Id'. I have obtained the Index number of rows which has morethan 5 NaN values by using 'for' loop. I stored every index number of rows in a 1D list. Then I am trying to delete rows according to index number one by one by implimenting another 'for' loop as shown below,

my dataframe name is = df_data_3 ###(164040 rows × 12 columns)

null_rows_index = [list of number of indexes] ### 261 number, that means 261 rows which has morethan 5 NaN values

j = 0
for j in range(len(null_rows_index)):
    df_data_3 = df_data_3.drop(index=[null_rows_index[j]], inplace = True)

but this shows an error while running as below,

'NoneType' object has no attribute 'drop'

How would I drop the multiple rows?

bleed_bits
  • 65
  • 5
  • 1
    Does this answer your question? [Why do I get AttributeError: 'NoneType' object has no attribute 'something'?](https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something) – Ulrich Eckhardt Mar 11 '22 at 21:57

1 Answers1

0

When you specify inplace, as you have, the operation occurs in place (no surprise), and the function returns None. So, you need:

    df_data_3.drop(index=[null_rows_index[j]], inplace = True)

The documentation makes this clear, and should have been your first stop.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • do you mean, I should do remove the variable 'df_data_3' in a for loop??? if yes, I removed and checked again but got the same error. and the same syntax I have used earlier which I already showed in the question. – bleed_bits Mar 11 '22 at 22:20
  • No, that's not at all what I said. I showed you exactly what to do. If you had cut-and-pasted my line, it would work. Remove `df_data_3 =` from your line. When you pass `inplace = True`, `.drop` modifies the list in memory. It doesn't return anything. – Tim Roberts Mar 12 '22 at 04:39