Home > Back-end >  Find out the sum of the null values for an attributes depending upon class values in pandas
Find out the sum of the null values for an attributes depending upon class values in pandas

Time:07-27

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.

CodePudding user response:

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
  • Related