Search code examples
pythonconditional-statementsmissing-datafillna

Filling Null Values in Pandas


How to fill missing values based on conditions meeting from other columns such as I want to fill only mull values of Product Container Column based on the Product Category column if it is "Office supplies" in Product Category it will be a "Small Box" and there are some other conditions as well?

cnt=0
for row in salesdirty["Product_Category"]:
    if salesdirty.loc[cnt,"Product_container"]==pd.isnull(salesdirty["Product_container"]):
        if salesdirty.loc[cnt,"Product_Category"]=="Office Supplies":
            salesdirty.loc[cnt,"Product_container"]="Small Box"
    cnt+=1

Solution

  • You could use Boolean indexing to filter the conditions, then do whatever you want with it:

    mask = (salesdirty["Product_container"].isnull()) & (salesdirty["Product_Category"]=="Office Supplies")
    
    # Assign new values
    salesdirty["Product_Category"][mask] = "Small Box"
    
    # Count conditions' occurences
    cnt = len(mask[mask])