熊猫系列groupby,一组

时间:2019-03-12 19:34:29

标签: python pandas pandas-groupby series

输入:

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

问题:

  1. 为什么我应该分成两组,但显然应该只有一组?
  2. 为什么我会从dataGB.sum()中获取垃圾,该怎么做才能获得预期的(以下)结果?
dataGB.sum()

预期结果:

>>> dataGB.sum()
date        name
2016-02-15  A       5
Name: N, dtype: int64

谢谢您的帮助!

0 个答案:

没有答案