#πŸ”’ Pandas filtering boolean mask

16 messages Β· Page 1 of 1 (latest)

spark violet
#

Hello guys, sorry to disturb you all; I'm learning some basics pandas filtering. I just learnt that we can use boolean masks (from what I've understood, a boolean mask is just a set of columns and rows where each cell is mask with a True/False value used to filter data - please do correct me if I'm wrong) Can someone explain why the following statement doesn't work while the other work please:


rewards_df[rewards_df.isna().sum() > 0] // didn't work


rewards_df.isna().sum()[rewards_df.isna().sum() > 0] // this one work

Why the first one is incorrect

radiant bayBOT
#

@spark violet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rose panther
#

I suspect you're looking for something like rewards_df.loc[:, rewards_df.isna().sum() > 0], perhaps

spark violet
#

ah I see, the : means every rows, we are filtering by the columns where we have only null values

#

but then hmm the .loc returns what I forgot 😭

rose panther
#

yeah, df[thing] is more like df.loc[thing, :]

spark violet
#

ah I see, so it will return a dataframe then, just 2sec I try it

#

ah I see the difference

#

in fact yes we get a dataframe, the dataframe of only null values

#

the difference with this one:

rewards_df.isna().sum()[rewards_df.isna().sum() > 0]

is that this return a series, where the rows are indexed with the column names; we won't have every column name though, only those where we have null values, then for each column we have their respective sum of null values

rose panther
#

yeah

clever dragon
radiant bayBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.