python 多索引上的日期时间groupby

9rbhqvlz  于 2023-01-11  发布在  Python
关注(0)|答案(1)|浏览(154)

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

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索引执行groupby,然后求和?我尝试了以下方法:

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

这显然行不通。

ccrfmcuu

ccrfmcuu1#

像这样?

df.groupby(df.index.get_level_values('alpha').month).number.sum()

相关问题