Advertisements
Advertisements
Question
Consider the following code:
>>>import pandas as pd >>>import numpy as np >>>s2=pd.
Series([12,np.nan,10]) >>>print(s2)
Find the output of the above code and write a Python statement to count and display only non-null values in the above series.
Short Answer
Advertisements
Solution
Printing the Series s2 displays the index numbers and values. Because the Series contains a missing value (np.nan), Pandas converts all integer values into floats (float64).
Output:
| 0 | 12.0 |
| 1 | NaN |
| 2 | 10.0 |
| dtype | float64 |
To count and display only the non-null values (ignoring the NaN), use the .count() method:
print(s2.count())
shaalaa.com
Is there an error in this question or solution?
