python填充空值_Python机器学习(九十一)Pandas 填充(Imputation)空值
除了刪除空值,還有一種處理空值的方法是填充(Imputation)。
讓我們看看在revenue_millions列中填充缺失的值。
首先,將該列提取到變量中:
revenue = movies_df[‘revenue_millions‘]
使用DataFrame[‘列名‘]可以獲取1列數(shù)據(jù),即一個Series。
可以使用head查看前面幾個數(shù)據(jù):
revenue.head()
輸出
Title
Guardians of the Galaxy333.13Prometheus126.46Split138.12Sing270.32Suicide Squad325.02Name: revenue_millions, dtype: float64
Series與DataFrame格式略有不同,但仍然有標(biāo)題索引。
填充空值,不能亂填,我們將使用列內(nèi)的均值來填充。這平均值:
revenue_mean =revenue.mean()
revenue_mean
輸出
82.95637614678898
取均值,用fillna()方法填充空值:
revenue.fillna(revenue_mean, inplace=True)
現(xiàn)在,用列的均值替換了所有的收益為null的項。注意,通過使用inplace=True,修改了原數(shù)據(jù)movies_df:
movies_df.isnull().sum()
輸出
rank 0
genre 0
description 0
director 0
actors 0
year 0
runtime 0
rating 0
votes 0
revenue_millions 0
metascore64dtype: int64
我們使用了整列均值來填充空值,還可以使用同個導(dǎo)演作品的均值、同類型電影的均值等等。
原文地址:https://www.cnblogs.com/huanghanyu/p/13174103.html
總結(jié)
以上是生活随笔為你收集整理的python填充空值_Python机器学习(九十一)Pandas 填充(Imputation)空值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @autowired注解_SpringB
- 下一篇: 2018python培训多年口碑_201