2887 Fill Missing Data

Problem Statement

DataFrame products

Column Name
Type

name

object

quantity

int

price

int

Write a solution to fill in the missing value as 0 in the quantity column.

The result format is in the following example.

For the whole problem statement, please refer here.

Plans

  • Use pandas to handle the data.

  • Fill in the missing values in the quantity column with 0.

  • Provide the DataFrame with the missing values filled.

  • Return the result.

Solution

import pandas as pd

def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame:
    # Fill missing values in the 'quantity' column with 0
    products['quantity'].fillna(value=0, inplace=True)

    # Return the updated DataFrame
    return products

Explanation

  1. Import Pandas

    • We start by importing the Pandas library, which provides data structures and operations for manipulating numerical tables and time series.

  2. Define the Function

    • We define a function fillMissingValues that takes a single argument products, which is a DataFrame containing product data.

  3. Filling Missing Values

    • We use the fillna method on the DataFrame products to fill in missing values in the quantity column with 0.

    • The value=0 argument specifies the value to use for filling in missing values.

    • The inplace=True argument specifies that the changes should be made directly to the DataFrame products.

  4. Return the Result

    • We return the updated DataFrame after filling in the missing values in the quantity column with 0.

Last updated