考虑以下示例,其中我们设置示例数据集,创建MultiIndex,取消堆叠数据帧,然后执行线性插值,我们逐行填充:
import pandas as pd # version 0.14.1
import numpy as np # version 1.8.1
df = pd.DataFrame({'location': ['a', 'b'] * 5,
'trees': ['oaks', 'maples'] * 5,
'year': range(2000, 2005) * 2,
'value': [np.NaN, 1, np.NaN, 3, 2, np.NaN, 5, np.NaN, np.NaN, np.NaN]})
df.set_index(['trees', 'location', 'year'], inplace=True)
df = df.unstack()
df = df.interpolate(method='linear', axis=1)
Run Code Online (Sandbox Code Playgroud)
未堆叠数据集的位置如下所示:
value
year 2000 2001 2002 2003 2004
trees location
maples b NaN 1 NaN 3 NaN
oaks a NaN 5 NaN NaN 2
Run Code Online (Sandbox Code Playgroud)
作为插值 …