<p>首先按<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html" rel="nofollow noreferrer">^{<cd1>}</a>转换列,然后按<code>MS</code>转换<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html" rel="nofollow noreferrer">^{<cd2>}</a>对于月初,最后将DatetimeIndex的格式更改为<code>MM/YYY</code>按<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DatetimeIndex.strftime.html" rel="nofollow noreferrer">^{<cd5>}</a>:</p>
<pre><code>df['datetime'] = pd.to_datetime(df['datetime'], format='%d/%m/%Y %H:%M')
df = df.resample('MS', on='datetime').mean()
df.index = df.index.strftime('%m/%Y')
print (df)
val1 val2 val3 val4 val5 val6 val7 val8 val9 \
06/2017 0.0 0.000000 0.0 0.0 0.0 0.000000 0.000000 0.0 0.0
07/2017 NaN NaN NaN NaN NaN NaN NaN NaN NaN
08/2017 0.0 0.143333 0.0 0.0 0.0 0.186667 0.183333 0.0 0.0
val10 val11 val12
06/2017 0.0115 0.0 0.035667
07/2017 NaN NaN NaN
08/2017 0.0120 0.0 0.034000
</code></pre>
<p>或者通过<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.strftime.html" rel="nofollow noreferrer">^{<cd6>}</a>将转换后的datetimes列传递给<code>groupby</code>并聚合<code>mean</code>:</p>
<pre><code>df = df.groupby(df['datetime'].dt.strftime('%m/%Y')).mean()
print (df)
val1 val2 val3 val4 val5 val6 val7 val8 val9 \
datetime
06/2017 0 0.000000 0 0 0 0.000000 0.000000 0 0
08/2017 0 0.143333 0 0 0 0.186667 0.183333 0 0
val10 val11 val12
datetime
06/2017 0.0115 0 0.035667
08/2017 0.0120 0 0.034000
</code></pre>