Skip to content
Activities · Activity 2.3

Q.Consider the following code:
import pandas as pd
import numpy as np
s2=pd.Series([12,np.nan,10])
print(s2)
Find output of the above code and write a Python statement to count and display only non null values in the above series.

Uttarakhand UbseTextbookSubjective· 3mImportance★★★★★
8% · 3/38 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The given code creates a Pandas Series with one np.nan value, and printing it shows the default index, values (with np.nan causing a float dtype), and data type. To count non-null values, use the .notnull() method followed by .sum().

When working with data, especially in real-world scenarios, it's common to encounter missing or undefined values. Pandas, a powerful library for data manipulation in Python, provides specific ways to handle such values, primarily through np.nan (Not a Number) from the NumPy library. Understanding how Pandas Series represents and processes these null values is crucial for accurate data analysis.

The problem asks us to first determine the output of a given Python code snippet that creates and prints a Pandas Series containing np.nan. Then, we need to write a Python statement to count and display only the non-null values in that Series.

Understanding the Initial Code

Let's break down the provided code step by step:

import pandas as pd
import numpy as np
s2=pd.Series([12,np.nan,10])
print(s2)
  1. import pandas as pd: This line imports the Pandas library, aliasing it as pd for convenience. Pandas is essential for creating and manipulating Series and DataFrames.
  2. import numpy as np: This line imports the NumPy library, aliasing it as np. NumPy is fundamental for numerical operations in Python and provides np.nan, which is the standard representation for missing or undefined numerical values in Pandas.
  3. s2=pd.Series([12,np.nan,10]): This line creates a Pandas Series named s2.
    • A Series is a one-dimensional labeled array capable of holding any data type.
    • The data provided is a list [12, np.nan, 10].
    • Pandas automatically assigns a default integer index starting from 0 if no explicit index is provided.
    • Crucially, because np.nan is a floating-point type, Pandas will upcast the entire Series to a float data type (float64) to accommodate np.nan. This means even the integer values 12 and 10 will be stored as 12.0 and 10.0.
  4. print(s2): This line prints the Series s2 to the console. The standard output for a Pandas Series includes its index, its values, and its data type (dtype).

Output of the Initial Code

Based on the explanation above, the print(s2) statement will produce the following output:

0    12.0
1     NaN
2    10.0
dtype: float64
Note

The dtype: float64 indicates that all elements in the Series are stored as 64-bit floating-point numbers. This happens because np.nan is a float, and a Series must have a uniform data type.

Counting Non-Null Values

To count and display only the non-null values in the Series s2, we can leverage Pandas' built-in methods for handling missing data.

The core idea is to first identify which values are not null, and then count them.

  1. Identify non-null values: The Series.notnull() method returns a boolean Series of the same shape as the original Series, where True indicates a non-null value and False indicates a null value.
    • For s2 = pd.Series([12, np.nan, 10]), s2.notnull() would result in a Series like:
      0     True
      1    False
      2     True
      dtype: bool
      
  2. Count the True values: In Python and Pandas, True is treated as 1 and False as 0 in arithmetic operations. Therefore, applying the .sum() method to a boolean Series will count the number of True values.
    • s2.notnull().sum() would sum True + False + True, which evaluates to 1 + 0 + 1 = 2. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.