擅长:python、mysql、java
<p>执行以下操作:</p>
<ol>
<li>按门店代码、周/小时排序</li>
<li>按0筛选</li>
<li>将减法存储在df['week_hr'][1:]之间。值df['week_hr'][:-1]。值之间,这样您就可以知道它们是否连续。在</li>
<li><p>现在,您可以将组设置为连续,并根据需要进行过滤。在</p>
<pre><code>import numpy as np
import pandas as pd
# 1
t1 = df.sort_values(['store_code', 'week_hr'])
# 2
t2 = t1[t1['baskets'] == 0]
# 3
continuous = t2['week_hr'][1:].values-t2['week_hr'][:-1].values == 1
groups = np.cumsum(np.hstack([False, continuous==False]))
t2['groups'] = groups
# 4
t3 = t2.groupby(['store_code', 'groups'], as_index=False)['week_hr'].count()
t4 = t3[t3.week_hr > 2]
print pd.merge(t2, t4[['store_code', 'groups']])
</code></pre></li>
</ol>
<p>不需要循环!在</p>