# 创建一个DataFrame
df = pd.DataFrame(np.random.randn(6,1),
index=pd.date_range('2013-08-01', periods=6, freq='B'), # 'B',即工作日
columns=list('A'))
df


Pandas cookbook - Missing Data_缺失值


# 使用loc方式将第4行,第A列值改为np.nan
df.loc[df.index[3], 'A'] = np.nan
df

Pandas cookbook - Missing Data_缺失值_02

Pandas dataframe.ffill()函数用于填充 DataFrame 中的缺失值。
“填充”代表“向前填充”,并将向前传播最后一个有效观察值。
# 使用reindex和ffill函数,将DataFrame先反向排序,再向前填充空值
df.reindex(df.index[::-1]).ffill() # df.index[::-1] 将index倒序排列

Pandas cookbook - Missing Data_缺失值_03