在 Pandas 中填充空值
Filling Null Values in Pandas
如何根据其他列满足的条件填充缺失值,例如我只想根据产品类别列填充产品容器列的混合值,如果它在产品类别中是 "Office supplies" 它将是a "Small Box" 还有其他一些条件吗?
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
您可以使用布尔索引来过滤条件,然后用它做任何您想做的事:
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])
如何根据其他列满足的条件填充缺失值,例如我只想根据产品类别列填充产品容器列的混合值,如果它在产品类别中是 "Office supplies" 它将是a "Small Box" 还有其他一些条件吗?
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
您可以使用布尔索引来过滤条件,然后用它做任何您想做的事:
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])