I'm trying to sum the duration of the activities using a for loop with conditions.
mydf= {'Duration' : [14, 8, 6, 36, 12, 5, 3, 2, 4, 5, 8, 3, 14, 1, 27, 25, 117, 2, 962, 2, 2, 1],
'Activity': ['Groom', 'Pause', 'Groom', 'Sleep', 'Sleep', 'Sleep', 'Sleep', 'Sleep', 'Sleep', 'Sleep', 'Sleep', 'Sleep', 'Sleep', 'Awaken', 'Groom', 'Pause', 'Groom', 'Eat', 'Cuddle', 'Come down', 'Dig', 'Forage']}
df = pd.DataFrame(mydf)
I want to sum all the sleep duration together.
I tried:
Sleep_sum = [sum(df['Duration'] for i in df['Activity'] if [i+1]=='Sleep')]
but this gives me an error : TypeError: can only concatenate str (not "int") to str.
I also tried this:
for i in range of len(df):
df['Activity'][i] == 'Sleep'
if [i] = [i+1]
df['Duration'].sum()
Essentially, I need to sum the duration of 'Sleep" if it's followed by another row == "Sleep".
Thank you for your time!