pandas multiindex dataframe, ND interpolation for missing values

前端 未结 3 1289
逝去的感伤
逝去的感伤 2021-01-16 21:00

Is it possible in pandas to interpolate for missing values in multiindex dataframe. This example below does not work as expected:

arr1=np.array(np.arange(1.,         


        
3条回答
  •  不思量自难忘°
    2021-01-16 21:22

    def multireindex(_df, new_multi_index, method='linear',copy=True):
        #from scipy.interpolate import griddata
        #import numpy as np
        #import pandas as pd
        _points=np.array(_df.index.values.tolist())
        dfn=dict()
        for aclm in _df.columns:
            dfn[aclm] = griddata(_points, _df[aclm], 
                            np.array(new_multi_index), method=method)
        dfn=pd.DataFrame(dfn,index=pd.MultiIndex.from_tuples(
                new_multi_index,names=_df.index.names))
        return pd.concat([dfn,_df])
    
    import pandas as pd
    import numpy as np
    #import numpy.random as npr
    #df1=pd.DataFrame(npr.rand(10,5))
    arr1=np.random.rand(100)
    arr2=np.random.rand(100)
    arr1,arr2=[np.round(a*b) for a,b in 
                    zip([arr1,arr2],[100,100,1000])]
    df1=pd.DataFrame(zip(arr1,arr2,arr1+arr2,arr1*arr2),columns=['x','y','plus','times'])
    df1.set_index(['x','y'],inplace=True)
    from scipy.interpolate import griddata
    new_points=[(20.0,20.0),(25.0,25.0)]
    df2=multireindex(df1,new_points)
    df2.head()
    

提交回复
热议问题