输入:
import pandas as pd
data = pd.DataFrame(data={'date':[pd.Timestamp('2016-02-15')]*3,
'time':[pd.Timedelta(x) for x in ('07:30:00','10:10:00','11:10:00')],'name':['A']*3, 'N':[1,2,3]}
).set_index(['date','time','name']).sort_index()
data = data[ data.index.get_level_values('time')>=pd.to_timedelta('09:30:00') ]
dataGB = data['N'].groupby(['date','name'])
print(data)
print('Number of groups:',len(dataGB))
print(dataGB.sum())
print(pd.__version__)
输出:
>>> print(data)
N
date time name
2016-02-15 10:10:00 A 2
11:10:00 A 3
>>> print('Number of groups:',len(dataGB))
Number of groups: 2
>>> print(dataGB.sum())
date 2
name 3
Name: N, dtype: int64
>>> print(pd.__version__)
0.24.1
问题:
dataGB.sum()
中获取垃圾,该怎么做才能获得预期的(以下)结果?dataGB.sum()
的预期结果:
>>> dataGB.sum()
date name
2016-02-15 A 5
Name: N, dtype: int64
谢谢您的帮助!