Python 使用另一列的滚动值的数据帧百分比

Python 使用另一列的滚动值的数据帧百分比,python,pandas,dataframe,percentile,Python,Pandas,Dataframe,Percentile,我有下面的数据帧结构作为示例 我想获得一个列,在该列中,它使用滚动n周期回溯,根据“百分位”列的值计算“价格列”的百分位 可能吗?我尝试使用某种lambda函数并使用.apply语法,但无法使其工作 date percentile price desired_row 2019-11-08 0.355556 0.6863 36th percentile of price of last n period 2019-11-11 0.316

我有下面的数据帧结构作为示例

我想获得一个列,在该列中,它使用滚动n周期回溯,根据“百分位”列的值计算“价格列”的百分位

可能吗?我尝试使用某种lambda函数并使用.apply语法,但无法使其工作

        date     percentile  price   desired_row
    2019-11-08  0.355556    0.6863    36th percentile of price of last n period
    2019-11-11  0.316667    0.6851    32nd percentile of price of last n period
    2019-11-12  0.305556    0.6841    ...
    2019-11-13  0.302778    0.6838    ...
    2019-11-14  0.244444    0.6798    ...

谢谢

您可以在pandas中使用滚动方法。例如:

df = pd.DataFrame({'B': [0, 1, 2, 2, 4]})
df['rolling_mean'] = df['B'].rolling(2).mean()
df['rolling_sum'] = df['B'].rolling(2).sum()

将创建“B”列的两个期间滚动平均值的新列。如果需要计算不同的汇总统计信息,可以应用不同的方法,例如:

df = pd.DataFrame({'B': [0, 1, 2, 2, 4]})
df['rolling_mean'] = df['B'].rolling(2).mean()
df['rolling_sum'] = df['B'].rolling(2).sum()

有关功能的更多信息,请参阅:

您可以在pandas中使用滚动方法。例如:

df = pd.DataFrame({'B': [0, 1, 2, 2, 4]})
df['rolling_mean'] = df['B'].rolling(2).mean()
df['rolling_sum'] = df['B'].rolling(2).sum()

将创建“B”列的两个期间滚动平均值的新列。如果需要计算不同的汇总统计信息,可以应用不同的方法,例如:

df = pd.DataFrame({'B': [0, 1, 2, 2, 4]})
df['rolling_mean'] = df['B'].rolling(2).mean()
df['rolling_sum'] = df['B'].rolling(2).sum()

有关功能的更多信息,请参阅:

基于,您可以使用
滚动
对列价格和索引中的列百分比进行滚动,然后在
应用
中使用参数
raw=False

window = 3
df['desired_row'] = df.set_index('percentile')['price'].rolling(window)\
                      .apply(lambda x: x.quantile(q=x.index[-1]), raw=False).values
print (df)
         date  percentile   price  desired_row
0  2019-11-08    0.355556  0.6863          NaN
1  2019-11-11    0.316667  0.6851          NaN
2  2019-11-12    0.305556  0.6841     0.684711
3  2019-11-13    0.302778  0.6838     0.683982
4  2019-11-14    0.244444  0.6798     0.681756
您可以根据需要更改
分位数中的
插值
参数。

基于,您可以使用
滚动
对列价格进行索引中的列百分比,然后在
应用
中使用参数
原始=假

window = 3
df['desired_row'] = df.set_index('percentile')['price'].rolling(window)\
                      .apply(lambda x: x.quantile(q=x.index[-1]), raw=False).values
print (df)
         date  percentile   price  desired_row
0  2019-11-08    0.355556  0.6863          NaN
1  2019-11-11    0.316667  0.6851          NaN
2  2019-11-12    0.305556  0.6841     0.684711
3  2019-11-13    0.302778  0.6838     0.683982
4  2019-11-14    0.244444  0.6798     0.681756

您可以根据需要更改
分位数中的
插值
参数。

这正是我需要的。谢谢这正是我需要的。谢谢