2016-04-25 68 views
0

我是新来的蟒蛇和学习熊猫。我有一个下面给出的数据集,需要在购买第一个GPS产品之前将Amount列中的所有行相加,并且在购买第一个GPS产品之后执行相同的操作,直到购买第二个GPS。在第n行之前和之后总结熊猫

这里的数据集:

df = pd.DataFrame({Product : [Sub,Sub,Sub,Sub,GPS,Sub,Sub,GPS,Sub,Sub]} {Amount :[13,15,25,32,43,76,23,45,67,89]}) 

感谢您您的帮助提前。

回答

0

我会用ILOC索引:

# locate first ocurence of purchase 
first_GPS_index = df[df['Product']==GPS].index.min() 
# use iloc to index same way as you would in python list 
sum_until = df.iloc[:first_GPS_index].sum() 
sum_after = df.iloc[first_GPS_index+1:].sum()