我正在寻找一种方法来做类似各种rolling_*函数的方法pandas,但我希望滚动计算的窗口由一系列值(比如,DataFrame的一列值的值)定义,而不是窗口中的行数.
举个例子,假设我有这些数据:
>>> print d
RollBasis ToRoll
0 1 1
1 1 4
2 1 -5
3 2 2
4 3 -4
5 5 -2
6 8 0
7 10 -13
8 12 -2
9 13 -5
Run Code Online (Sandbox Code Playgroud)
如果我做了类似的事情rolling_sum(d, 5),我得到一个滚动的总和,其中每个窗口包含5行.但我想要的是滚动总和,其中每个窗口包含一定范围的值RollBasis.也就是说,我希望能够做类似的事情d.roll_by(sum, 'RollBasis', 5),并得到一个结果,其中第一个窗口包含RollBasis1到5之间的所有行,然后第二个窗口包含所有RollBasis在2到6之间的行,然后是第三个window包含RollBasis3到7之间的所有行,等等.窗口的行数不会相等,但RollBasis每个窗口中选择的值范围将相同.所以输出应该是这样的:
>>> d.roll_by(sum, 'RollBasis', 5)
1 -4 # sum of elements with 1 <= Rollbasis <= 5
2 -4 # sum of elements with 2 <= Rollbasis <= 6
3 -6 # sum of elements with 3 <= Rollbasis <= 7
4 -2 # sum of elements with 4 <= Rollbasis <= 8
# etc.
Run Code Online (Sandbox Code Playgroud)
我不能这样做groupby,因为groupby总是产生不相交的群体.我无法使用滚动功能,因为它们的窗口总是按行数滚动,而不是按值.那我该怎么办呢?
Zel*_*ny7 15
我认为这样做你想要的:
In [1]: df
Out[1]:
RollBasis ToRoll
0 1 1
1 1 4
2 1 -5
3 2 2
4 3 -4
5 5 -2
6 8 0
7 10 -13
8 12 -2
9 13 -5
In [2]: def f(x):
...: ser = df.ToRoll[(df.RollBasis >= x) & (df.RollBasis < x+5)]
...: return ser.sum()
Run Code Online (Sandbox Code Playgroud)
上面的函数接受一个值,在本例中为RollBasis,然后根据该值索引数据框列ToRoll.返回的系列包含符合RollBasis + 5标准的ToRoll值.最后,对该系列进行求和并返回.
In [3]: df['Rolled'] = df.RollBasis.apply(f)
In [4]: df
Out[4]:
RollBasis ToRoll Rolled
0 1 1 -4
1 1 4 -4
2 1 -5 -4
3 2 2 -4
4 3 -4 -6
5 5 -2 -2
6 8 0 -15
7 10 -13 -20
8 12 -2 -7
9 13 -5 -5
Run Code Online (Sandbox Code Playgroud)
其他人想要尝试的玩具示例DataFrame的代码:
In [1]: from pandas import *
In [2]: import io
In [3]: text = """\
...: RollBasis ToRoll
...: 0 1 1
...: 1 1 4
...: 2 1 -5
...: 3 2 2
...: 4 3 -4
...: 5 5 -2
...: 6 8 0
...: 7 10 -13
...: 8 12 -2
...: 9 13 -5
...: """
In [4]: df = read_csv(io.BytesIO(text), header=0, index_col=0, sep='\s+')
Run Code Online (Sandbox Code Playgroud)
Bre*_*arn 14
基于Zelazny7的答案,我创建了这个更通用的解决方案:
def rollBy(what, basis, window, func):
def applyToWindow(val):
chunk = what[(val<=basis) & (basis<val+window)]
return func(chunk)
return basis.apply(applyToWindow)
>>> rollBy(d.ToRoll, d.RollBasis, 5, sum)
0 -4
1 -4
2 -4
3 -4
4 -6
5 -2
6 -15
7 -20
8 -7
9 -5
Name: RollBasis
Run Code Online (Sandbox Code Playgroud)
它仍然不理想,因为它相比很慢rolling_apply,但也许这是不可避免的.
Ian*_*ery 11
基于BrenBarns的答案,但是通过使用基于标签的索引而不是基于布尔的索引来加速:
def rollBy(what,basis,window,func,*args,**kwargs):
#note that basis must be sorted in order for this to work properly
indexed_what = pd.Series(what.values,index=basis.values)
def applyToWindow(val):
# using slice_indexer rather that what.loc [val:val+window] allows
# window limits that are not specifically in the index
indexer = indexed_what.index.slice_indexer(val,val+window,1)
chunk = indexed_what[indexer]
return func(chunk,*args,**kwargs)
rolled = basis.apply(applyToWindow)
return rolled
Run Code Online (Sandbox Code Playgroud)
这是很多比不使用索引列更快:
In [46]: df = pd.DataFrame({"RollBasis":np.random.uniform(0,1000000,100000), "ToRoll": np.random.uniform(0,10,100000)})
In [47]: df = df.sort("RollBasis")
In [48]: timeit("rollBy_Ian(df.ToRoll,df.RollBasis,10,sum)",setup="from __main__ import rollBy_Ian,df", number =3)
Out[48]: 67.6615059375763
In [49]: timeit("rollBy_Bren(df.ToRoll,df.RollBasis,10,sum)",setup="from __main__ import rollBy_Bren,df", number =3)
Out[49]: 515.0221037864685
Run Code Online (Sandbox Code Playgroud)
值得注意的是,基于索引的解决方案是O(n),而逻辑切片版本在平均情况下是O(n ^ 2)(我认为).
我发现在从基数的最小值到基数的最大值的均匀间隔的窗口上执行此操作更有用,而不是在每个基础值上.这意味着改变功能:
def rollBy(what,basis,window,func,*args,**kwargs):
#note that basis must be sorted in order for this to work properly
windows_min = basis.min()
windows_max = basis.max()
window_starts = np.arange(windows_min, windows_max, window)
window_starts = pd.Series(window_starts, index = window_starts)
indexed_what = pd.Series(what.values,index=basis.values)
def applyToWindow(val):
# using slice_indexer rather that what.loc [val:val+window] allows
# window limits that are not specifically in the index
indexer = indexed_what.index.slice_indexer(val,val+window,1)
chunk = indexed_what[indexer]
return func(chunk,*args,**kwargs)
rolled = window_starts.apply(applyToWindow)
return rolled
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16406 次 |
| 最近记录: |