Interpolate between two nearby rows of Dataframe

淺唱寂寞╮ 提交于 2020-08-10 20:12:05

问题


I would like to interpolate missing values within groups in dataframe using preceding and following rows value.

Here is the df (there are more records within a group but for this example I left 3 per group):

import numpy as np
import pandas as pd
df = pd.DataFrame({'Group': ['a','a','a','b','b','b','c','c','c'],'Yval': [1,np.nan,5,2,np.nan,8,5,np.nan,10],'Xval': [0,3,2,4,5,8,3,1,9],'PTC': [0,1,0,0,1,0,0,1,0]})

df:

    Group   Yval    Xval    PTC
0   a       1.0     0       0
1   a       NaN     3       1
2   a       5.0     2       0
3   b       2.0     4       0
4   b       NaN     5       1
5   b       8.0     8       0
6   c       5.0     3       0
7   c       NaN     1       1
8   c       10.0    9       0

For PTC (point to calculate) I need Yval interpolation using Xval,Yval from -1, +1 rows. I.e. for A Group I would like: df.iloc[1,1]=np.interp(3, [0,2], [1,5])

Here is what I tried to do using loc and shift method and interp function found in this post:

df.loc[(df['PTC'] == 1), ['Yval']]= \
np.interp(df['Xval'], (df['Xval'].shift(+1),df['Xval'].shift(-1)),(df['Yval'].shift(+1),df['Yval'].shift(-1)))

Error I get:

ValueError: object too deep for desired array

回答1:


df['Xval-1'] = df['Xval'].shift(-1)
df['Xval+1'] = df['Xval'].shift(+1)
df['Yval-1'] = df['Yval'].shift(-1)
df['Yval+1'] = df['Yval'].shift(+1)

df["PTC_interpol"] = df.apply(lambda x: np.interp(x['Xval'], [x['Xval-1'], x['Xval+1']], [x['Yval-1'], x['Yval+1']]), axis=1)

df['PTC'] = np.where(df['PTC'].isnull(), df["PTC_interpol"], df['PTC'])


来源:https://stackoverflow.com/questions/63279957/interpolate-between-two-nearby-rows-of-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!