NaN values in pandas objects

We can use the isnull() function from the pandas library to identify NaN values:

  1. Check the following example:
dfx.isnull()

The output of the preceding code is as follows:

Note that the True values indicate the values that are NaN. Pretty obvious, right? Alternatively, we can also use the notnull() method to do the same thing. The only difference would be that the function will indicate True for the values which are not null.

  1. Check it out in action:
dfx.notnull()

And the output of this is as follows:

Compare these two tables. These two functions, notnull() and isnull(), are the complement to each other. 

  1. We can use the sum() method to count the number of NaN values in each store. How does this work, you ask? Check the following code:
dfx.isnull().sum()

And the output of the preceding code is as follows:

store1 1
store2 1
store3 1
store4 5
store5 7
dtype: int64

The fact that True is 1 and False is 0 is the main logic for summing. The preceding results show that one value was not reported by store1, store2, and store3. Five values were not reported by store4 and seven values were not reported by store5.

  1. We can go one level deeper to find the total number of missing values:
dfx.isnull().sum().sum()

And the output of the preceding code is as follows:

15

This indicates 15 missing values in our stores. We can use an alternative way to find how many values were actually reported.

  1. So, instead of counting the number of missing values, we can count the number of reported values:
dfx.count()

And the output of the preceding code is as follows:

store1 6
store2 6
store3 6
store4 2
store5 0
dtype: int64

Pretty elegant, right? We now know two different ways to find the missing values, and also how to count the missing values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.37.12