Skip to content

2887. Fill Missing Data

DataFrame products

Column NameType
nameobject
quantityint
priceint

Instructions

  • Write a solution to fill in the missing value as 0 in the quantity column.
  • The result format is in the following example.

Example

Input:

namequantityprice
WristwatchNone135
WirelessEarbudsNone821
GolfClubs7799319
Printer8493051

Output:

namequantityprice
Wristwatch0135
WirelessEarbuds0821
GolfClubs7799319
Printer8493051

Explanation:

The quantity for Wristwatch and WirelessEarbuds are filled by 0.

Submissions

python
import pandas as pd

def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame:
    products['quantity'] = products['quantity'].fillna(0)
    return products

Explanation

Python (Pandas)
Submitted by @noeyislearning
  • import pandas as pd: Import the pandas library to work with DataFrames.
  • def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame: Define a function that takes a DataFrame as input and returns a DataFrame.
  • products['quantity'] = products['quantity'].fillna(0): Fill the missing values in the quantity column with 0.
  • return products: Return the updated DataFrame.