Skip to content

Latest commit

 

History

History
56 lines (49 loc) · 1.65 KB

2887_fill_missing_data.md

File metadata and controls

56 lines (49 loc) · 1.65 KB

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.

Example 1:

Input:+-----------------+----------+-------+
| name            | quantity | price |
+-----------------+----------+-------+
| Wristwatch      | None     | 135   |
| WirelessEarbuds | None     | 821   |
| GolfClubs       | 779      | 9319  |
| Printer         | 849      | 3051  |
+-----------------+----------+-------+
Output:
+-----------------+----------+-------+
| name            | quantity | price |
+-----------------+----------+-------+
| Wristwatch      | 0        | 135   |
| WirelessEarbuds | 0        | 821   |
| GolfClubs       | 779      | 9319  |
| Printer         | 849      | 3051  |
+-----------------+----------+-------+
Explanation: 
The quantity for Wristwatch and WirelessEarbuds are filled by 0.

Solution

import pandas as pd


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


if __name__ == '__main__':
    data = [['Wristwatch', None, 135],
            ['WirelessEarbuds', None, 821],
            ['GolfClubs', 779, 9319],
            ['Printer', 849, 3051]]
    products = pd.DataFrame(data, columns=['name', 'quantity', 'price'])
    print(fillMissingValues(products))