编写 Python 代码来填充给定数据框中的所有缺失值
pythonpandasserver side programmingprogramming更新于 2024/6/22 21:00:17
解决方案
为了解决这个问题,我们将遵循下面给出的步骤 −
定义数据框
在方法 =’linear’、limit_direction =’forward’ 内应用 df.interpolate 函数并填充 NaN limit = 2
df.interpolate(method ='linear', limit_direction ='forward', limit = 2
示例
import pandas as pd
df = pd.DataFrame({"Id":[1, 2, 3, None, 5],
"Age":[12, 12, 14, 13, None],
&"Mark":[80, 90, None, 95, 85],
})
print("Dataframe is:\n",df)
print("Interpolate missing values:")
print(df.interpolate(method ='linear', limit_direction ='forward', limit = 2))
输出
Dataframe is: Id Age Mark 0 1.0 12.0 80.0 1 2.0 12.0 90.0 2 3.0 14.0 NaN 3 NaN 13.0 95.0 4 5.0 NaN 85.0 插入缺失值: Id Age Mark 0 1.0 12.0 80.0 1 2.0 12.0 90.0 2 3.0 14.0 92.5 3 4.0 13.0 95.0 4 5.0 13.0 85.0
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

