相关疑难解决方法(0)

DataFrame.interpolate()推断尾随丢失的数据

考虑以下示例,其中我们设置示例数据集,创建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)

作为插值 …

python interpolation pandas

12
推荐指数
2
解决办法
2481
查看次数

标签 统计

interpolation ×1

pandas ×1

python ×1