编写 Python 代码来填充给定数据帧中的所有缺失值
解决方案
为了解决这个问题,我们将按照下面给出的步骤-
定义数据框
df.interpolate在方法中应用函数='linear',limit_direction='forward'并填充NaNlimit=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("Interpolatemissingvalues:")
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 Interpolatemissingvalues: 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
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短