Masking using pandas DF

Why does the code in 1) work but not the code in 2), how do I make code 2) work?

mask = f500.loc[:,‘previous_rank’]==0

1) zero_previous_rank = f500[mask].shape[0]

2) zero_previous_rank = f500.loc[:,mask].shape[0]

1 Like

Hi and welcome back to the community!
Could you, please, format you post according these Guidelines so that we could help you better?
The most important points would be including the screen link and code formatting.
Also, you state that the 2nd code is not working. What do you mean by not working? Is the output not the one you expected? Does it raise some error? If so, please, include the whole error message as well.

2 Likes

Do

zero_previous_rank = f500.loc[mask, :].shape[0]

loc first one is stand for rows and second for column labels like df.loc[rows_index, column_index].
It was giving error because you are indexing column with rows boolean mask.

PS: Do follow guideline for future questions suggested by @ksenia.kustanovich to get quick answer and avoid confusion.

1 Like