<p>这里有一个稍微不同的方法:使用<code>year</code>和<code>month</code>来构建索引,然后使用一个UDF <code>groupby()</code>。在</p>
<p>示例数据:</p>
<pre><code>N = 10
years = pd.date_range("1981", "2017", freq="A").year
dates = np.random.choice(years, size=N, replace=True)
months = np.random.choice(range(1,13), size=N, replace=True)
inches = np.random.randint(1,20, size=N)
mm = np.random.randint(1,100, size=N)
data = {"year":dates, "month":months, "inch":inches, "mm":mm}
df = pd.DataFrame(data)
df
inch mm month year
0 19 31 12 1990
1 8 71 9 1986
2 5 85 2 2009
3 17 8 12 2005
4 10 14 12 1987
5 7 87 2 1982
6 8 59 2 2004
7 8 74 8 2016
8 5 6 6 1993
9 3 7 12 1982
</code></pre>
<p>现在根据年份进行子集并构建索引:</p>
^{pr2}$
<p>然后使用<code>year</code>和月份分隔函数groupby:</p>
<pre><code>def month_gb(x):
if x.month in range(3,6):
return 'MAM'
elif x.month in range(6,10):
return 'JJAS'
elif x.month in range(10,13):
return 'OND'
df.loc[mask].groupby(["year", month_gb]).mm.sum()
year
1987 OND 14
1990 OND 31
1993 JJAS 6
2005 OND 8
2016 JJAS 74
Name: mm, dtype: int64
</code></pre>