Does Series.mask() allow for .iloc series selection?

Screen Link:

My Code:

injured.iloc[3] = injured.iloc[3].mask(injured.iloc[3] != injured_manual_sum, np.nan)

What I expected to happen:
the ‘total_injured’ series, where the values that were not identical to the ‘injured_manual_sum’ values, would be replaced with a null value.

What actually happened:

ValueError: Can only compare identically-labeled Series objects

this code works as expected when I replace injured.iloc[3] with injured['total_injured'] even if only inside the Series.mask() method. I thought that those two names are just different ways to call the same series and should be interchangeable. Is there something else that I am missing? Thanks!

Hi @ncarvey

When you are using injured.iloc[3], you are actually selecting the 3rd row of your dataframe, not the 3rd column.

If you want to select the 3rd column (which is total_injured) with iloc, you have to use injured.iloc[:, 3], which means selecting all rows from the 3rd column. This piece of code is equivalent to using injured.loc[:, 'total_injured'] or injured['total_injured']).

Oh man, I feel so dumb. Ha! Thank you.

1 Like