pandas 是否可以在多索引数据帧中插入缺失值。下面的示例没有按预期工作:
arr1=np.array(np.arange(1.,10.,1.))
arr2=np.array(np.arange(2.,20.,2.))
df1=pd.DataFrame(zip(arr1,arr2,arr1+arr2,arr1*arr2),columns=['x','y','xplusy','xtimesy'])
df1.set_index(['x','y'],inplace=True)
df2=df1.reindex(index=zip(*df1.index.levels)+[(2,2),(3,2),(5,5)])
df2.sortlevel([0,1],inplace=True)
df2.interpolate(method='linear',inplace=True)
对于添加的索引,在 xplusy 和 xtimesy 列中显示的内容不是我所期望的。
----------- ---- ---
(1.0, 2.0) 3 2
(2.0, 2.0) 4.5 5
(2.0, 4.0) 6 8
(3.0, 2.0) 7.5 13
(3.0, 6.0) 9 18
(4.0, 8.0) 12 32
(5.0, 5.0) 13.5 41
(5.0, 10.0) 15 50
(6.0, 12.0) 18 72
(7.0, 14.0) 21 98
(8.0, 16.0) 24 128
(9.0, 18.0) 27 162
----------- ---- ---