0

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
Poojan
  • 3,366
  • 2
  • 17
  • 33
  • 1
    provide sample dataframe and expected output to help debug your code. Also clearly define your problem. – Poojan Nov 15 '19 at 21:25

1 Answers1

1

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])
fsl
  • 3,250
  • 1
  • 10
  • 20