任何人都有明智的方法来操纵连锁熊猫系列的价值以找到总数?
当前数据(类型:pandas.core.series.Series
)
仅供参考,此数据是通过使用原始数据中的“groupby”函数生成的。
date device
2015-07-08 a 0
b 0
c 0
d 1
2015-07-09 a 0
c 1
d 1
2015-07-10 a 1
b 1
c 1
预期结果(类型:pandas.core.series.Series
)
每个设备的值表示最新的计数总数A.
例如,总计(2015-07-10,c)= 2因为(2015-07-09,c)= 1和(2015-07-10,c)= 1
date device
2015-07-08 a 0
b 0
c 0
d 1
2015-07-09 a 0
c 1
d 2
2015-07-10 a 1
b 1
c 2
答案 0 :(得分:2)
我认为你可以使用DataFrameGroupBy.cumsum
和groupby
第二级:
dates = pd.DatetimeIndex(['2015-07-08','2015-07-08','2015-07-08','2015-07-08',
'2015-07-09','2015-07-09','2015-07-09',
'2015-07-10','2015-07-10','2015-07-10'])
devices = ['a','b','c','d','a','c','d','a','b','c']
idx = pd.MultiIndex.from_tuples(list(zip(dates, devices)), names=['date', 'device'])
s = pd.Series([0,0,0,1,0,1,1,1,1,1], index= idx)
print (s)
date device
2015-07-08 a 0
b 0
c 0
d 1
2015-07-09 a 0
c 1
d 1
2015-07-10 a 1
b 1
c 1
dtype: int64
print (s.groupby(level=1).cumsum())
date device
2015-07-08 a 0
b 0
c 0
d 1
2015-07-09 a 0
c 1
d 2
2015-07-10 a 1
b 1
c 2
dtype: int64