在两个相邻的数据框行之间插值 [英] Interpolate between two nearby rows of Dataframe
本文介绍了在两个相邻的数据框行之间插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用前后的行值对数据框中各组内的缺失值进行插值.
I would like to interpolate missing values within groups in dataframe using preceding and following rows value.
这是df(一个组中有更多记录,但在本示例中,我每组保留3条记录):
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
对于PTC(要计算的点),我需要使用-1,+ 1行中的Xval,Yval进行Yval插值.
IE.对于A组,我希望:
df.iloc[1,1]=np.interp(3, [0,2], [1,5])
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])
这是我尝试使用loc和shift方法执行的操作 并在此帖子中找到的插入函数:
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)))
我得到的错误:
ValueError: object too deep for desired array
推荐答案
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'])
这篇关于在两个相邻的数据框行之间插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文