Dropping rows in DataFrame resulting in ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Hi, i tried out a different method to drop the rows with null values as required in the fourth video of week 10 but ran into an error. The code went something like this:

for i in df.index:
if pd.isnull(df).any(axis = 1):
df.drop(i, inplace = True)

The error said: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I did not quite get it and would be glad to know if there is a way in which it could work
Thank you!

Try this:

for i in df.index:
    if pd.isnull(df).any(axis=1)[i]:
            df.drop(index=i, inplace=True)
1 Like

you just have to add one thing there in your if statement, so that each row will be iterated one by one…you can check it here:-

for i in df.index:
  if pd.isnull(df).any(axis = 1)[i]:
    df.drop(i, inplace = True)

your code is not iterating over each row (even you’re using for loop) that’s why you’re getting an error :slightly_smiling_face: :slightly_smiling_face:

1 Like