在multiindex上的datetime groupby

时间:2016-11-16 17:32:05

标签: python pandas

如果我有多索引设置如下:

import matplotlib.pyplot as plt

import pandas as pd
import numpy as np
from io import StringIO

csv = u"""string,date,number
a string1,2/5/11 9:16am,1.0
a string2,3/5/11 10:44pm,2.0
a string3,4/22/11 12:07pm,3.0
a string4,4/22/11 12:10pm,4.0
a string5,4/29/11 11:59am,1.0
a string6,5/2/11 1:41pm,2.0
a string7,5/2/11 2:02pm,3.0
a string8,5/2/11 2:56pm,4.0
a string9,5/2/11 3:00pm,5.0
a string10,5/2/14 3:02pm,6.0
a string11,5/2/14 3:18pm,7.0"""

df = pd.read_csv(StringIO(csv))
df['date']=pd.to_datetime(df['date'],format='%m/%d/%y %I:%M%p')
df.index = df['date']
df.index = pd.MultiIndex.from_tuples(zip(df['date'], df['string']), names=['alpha', 'bravo'])

如何按月对alpha索引进行分组,然后求和?我试过的是:

df.groupby(level='alpha').sum().groupby(df.index.month).sum()

显然不起作用。

0 个答案:

没有答案