Find out the sum of the null values for an attributes depending upon class values in pandas

Question:

The dataset looks like

 class       subject   computer_buy(class)
 Junior      Science   Yes
 Sophomore   Science   Yes
 Senior      Economics No
 Junior       ?        No
 Senior      Arts      No

Suppose the name of the dataset is toy_data. I want to know the how many ? values are there for the class value No

I wrote a query like that

   toy_data['subject'].isnull().sum()[toy_data['computer_buy']=='No']

The error for the above query

IndexError: invalid index to scalar variable.

What should be the right approach towards the query?

Thank you.

Asked By: Encipher

||

Answers:

toy_data['subject'].isnull().sum() returns a scalar value like 3, 1 so you can’t indexing it with [].

IIUC, you can use

out = ((toy_data['computer_buy']=='No') & (toy_data['subject'] == '?')).sum()
print(out)

1
Answered By: Ynjxsjmh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.