<p>有默认索引,因此可以使用楼层划分:</p>
<pre><code>df['cumulative_expected'] = df['Expected_response'].groupby(df.index // 86400).cumsum()
</code></pre>
<p>通常解决方案是使用楼层划分创建<code>np.arange</code>:</p>
^{pr2}$
<p>应通过比较<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.shift.html" rel="nofollow noreferrer">^{<cd2>}</a>ed值与<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.cumsum.html" rel="nofollow noreferrer">^{<cd3>}</a>来更改解决方案:</p>
<pre><code>s = (df['date']!=df['date'].shift()).cumsum()
df['cumulative_expected'] = df['Expected_response'].groupby(s).cumsum()
</code></pre>
<p>使用更改的样本数据进行测试:</p>
<pre><code>print (df)
date Expected_response
0 2018-03-01 0.270
1 2018-03-01 0.260
2 2018-03-02 0.240
3 2018-03-02 0.224
4 2018-03-02 0.204
5 2018-03-01 0.194
6 2018-03-01 0.190
s = (df['date']!=df['date'].shift()).cumsum()
print (s)
0 1
1 1
2 2
3 2
4 2
5 3
6 3
Name: date, dtype: int32
df['cumulative_expected'] = df['Expected_response'].groupby(s).cumsum()
print (df)
date Expected_response cumulative_expected
0 2018-03-01 0.270 0.270
1 2018-03-01 0.260 0.530
2 2018-03-02 0.240 0.240
3 2018-03-02 0.224 0.464
4 2018-03-02 0.204 0.668
5 2018-03-01 0.194 0.194
6 2018-03-01 0.190 0.384
</code></pre>