将单个数据帧值与同一列中的前10个值进行比较

时间:2015-10-22 20:01:45

标签: python pandas conditional dataframe vectorization

在数据框中,我想算一下过去10天的价格中有多少比今天的价格高。结果如下所示:

price   ct>prev10
50.00   
51.00   
52.00   
50.50   
51.00   
50.00   
50.50   
53.00   
52.00   
49.00   
51.00   3

我已经看到DSM回答了这篇文章,但要求的不同之处在于比较基数是静态数字而不是当前行:

Achieving "countif" with pd.rolling_sum()

当然我想在没有循环1x1的情况下这样做。非常难过 - 提前感谢任何建议。

2 个答案:

答案 0 :(得分:4)

您可以在系列中使用rolling_apply功能。考虑到样本数据的小尺寸,我使用了5的窗口长度,但您可以轻松地更改它。

lambda函数计算滚动组中的项目数(不包括最后一项)大于最后一项。

df = pd.DataFrame({'price': [50, 51, 52, 50.5, 51, 50, 50.5, 53, 52, 49, 51]})

window = 5  # Given that sample data only contains 11 values.
df['price_count'] = pd.rolling_apply(df.price, window, 
                                     lambda group: sum(group[:-1] > group[-1]))
>>> df
    price  price_count
0    50.0          NaN
1    51.0          NaN
2    52.0          NaN
3    50.5          NaN
4    51.0            1
5    50.0            4
6    50.5            2
7    53.0            0
8    52.0            1
9    49.0            4
10   51.0            2

在上面的示例中,第一组是索引值为0-4的价格。你可以看到发生了什么:

group = df.price[:window].values
>>> group
array([ 50. ,  51. ,  52. ,  50.5,  51. ])

现在,将前四个价格与当前价格进行比较:

>>> group[:-1] > group[-1]
array([False, False,  True, False], dtype=bool)

然后,你只是总结布尔值:

>>> sum(group[:-1] > group[-1])
1

这是放入索引4的第一个关闭窗口的值。

答案 1 :(得分:1)

这是一个带NumPy模块的vectoized方法,支持broadcasting实现矢量化方法 -

import numpy as np
import pandas as pd

# Sample input dataframe
df = pd.DataFrame({'price': [50, 51, 52, 50.5, 51, 50, 50.5, 53, 52, 49, 51]})

# Convert to numpy array for counting purposes
A = np.array(df['price'])

W = 5 # Window size

# Initialize another column for storing counts
df['price_count'] = np.nan

# Get counts and store as a new column in dataframe
C = (A[np.arange(A.size-W+1)[:,None] + np.arange(W-1)] > A[W-1:][:,None]).sum(1)
df['price_count'][W-1:] = C

示例运行 -

>>> df
    price
0    50.0
1    51.0
2    52.0
3    50.5
4    51.0
5    50.0
6    50.5
7    53.0
8    52.0
9    49.0
10   51.0
>>> A = np.array(df['price'])
>>> W = 5 # Window size
>>> df['price_count'] = np.nan
>>> 
>>> C=(A[np.arange(A.size-W+1)[:,None] + np.arange(W-1)] > A[W-1:][:,None]).sum(1)
>>> df['price_count'][W-1:] = C
>>> df
    price  price_count
0    50.0          NaN
1    51.0          NaN
2    52.0          NaN
3    50.5          NaN
4    51.0            1
5    50.0            4
6    50.5            2
7    53.0            0
8    52.0            1
9    49.0            4
10   51.0            2