替换缺失值
处理缺失值的另一种方式是将它们全部替换为同一个值。对于数值型变量,可以选择替换为 0——本题就这样做。不过,替换缺失值时,您实际上是在对"缺失"代表的含义做出假设。本例中,您将假设"售出数量缺失"表示该周这种鳄梨没有任何销量。
在本练习中,您将通过直方图观察替换缺失值如何影响变量的分布。您可以一次为多个变量绘制直方图,例如:
dogs[["height_cm", "weight_kg"]].hist()
已将 pandas 导入为 pd,将 matplotlib.pyplot 导入为 plt。数据集 avocados_2016 可用。
本练习是课程的一部分
使用 pandas 进行数据处理
交互式实操练习
通过完成这段示例代码来试试这个练习。
# List the columns with missing values
cols_with_missing = ["small_sold", "large_sold", "xl_sold"]
# Create histograms showing the distributions cols_with_missing
avocados_2016[____].____
# Show the plot
____