Is there any function in pandas to simulate excel formula like '=sum($A$1:A10'(for 10th row), i.e. the formula should take rolling data from 1st row.
Pandas rolling function needs a integer value as window argument.
Is there any function in pandas to simulate excel formula like '=sum($A$1:A10'(for 10th row), i.e. the formula should take rolling data from 1st row.
Pandas rolling function needs a integer value as window argument.
The equivalent of =SUM($A$1:A1)
in pandas is .expanding().sum()
(requires pandas 0.18.0):
ser = pd.Series([1, 2, 3, 4])
ser
Out[3]:
0 1
1 2
2 3
3 4
dtype: int64
ser.expanding().sum()
Out[4]:
0 1.0
1 3.0
2 6.0
3 10.0
You can also apply a generic function via apply:
ser.expanding().apply(lambda x: np.percentile(x, 90))
Out:
0 1.0
1 1.9
2 2.8
3 3.7
dtype: float64
Or directly with quantile:
ser.expanding().quantile(0.9)
Out[15]:
0 1.0
1 1.0
2 2.0
3 3.0
dtype: float64
Note that 90th percentile is equal to 0.9th quantile. However, Series.quantile and Series.expanding.quantile are returning different results which is probably a bug.
np.percentile returns the same results as Excel's PERCENTILE.INC. For PERCENTILE.EXC, I've previously wrote a small function here.