<p>您可以在<code>Grouper</code>中使用<code>level</code>关键字。(另请注意,<code>TimeGrouper</code>已弃用)。这个参数是</p>
<blockquote>
<p>the level for the target index.</p>
</blockquote>
<p>示例数据帧:</p>
<pre><code>dates = pd.date_range('2017-01', freq='10MIN', periods=1000)
strs = ['aa'] * 1000
df = pd.DataFrame(np.random.rand(1000,2), index=pd.MultiIndex.from_arrays((dates, strs)))
</code></pre>
<p>解决方案:</p>
^{pr2}$
<p>更新:您在评论中指出,您希望删除的结果计数为零。例如,假设您的数据帧实际上丢失了几天:</p>
<pre><code>df = df.drop(df.index[140:400])
print(df.groupby(pd.Grouper(freq='D', level=0)).count())
0 1
2017-01-01 140 140
2017-01-02 0 0
2017-01-03 32 32
2017-01-04 144 144
2017-01-05 144 144
2017-01-06 144 144
2017-01-07 136 136
</code></pre>
<p>据我所知,没有办法排除<code>.count</code>内的零计数。相反,您可以使用上面的结果来删除0。在</p>
<p>第一个解决方案(可能不太可取,因为当引入<code>np.nan</code>时,它将和<code>int</code>结果转换为<code>float</code>,这可能是</p>
<pre><code>res = df.groupby(pd.Grouper(freq='D', level=0)).count()
res = res.replace(0, np.nan).dropna()
</code></pre>
<p>在我看来,第二个更好的解决方案是<a href="https://stackoverflow.com/questions/22649693/drop-rows-with-all-zeros-in-pandas-data-frame">here</a>:</p>
<pre><code>res = res[(res.T != 0).any()]
print(res) # notice - excludes 2017-01-02
0 1
2017-01-01 140 140
2017-01-03 32 32
2017-01-04 144 144
2017-01-05 144 144
2017-01-06 144 144
2017-01-07 136 136
</code></pre>
<p><code>.any</code>来自NumPy,被移植到pandas,当请求的轴上有任何元素为True时返回True。在</p>