Reputation: 107
I have a subset of a dataframe here:
data = {'Name': ['ch1', 'ch2', 'ch3', 'ch4', 'ch5', 'ch6'],
'Time': [1,2,3,4,5,6],
'Week' : [1, 2, 3, 2, 3, 2]
}
dfx = pd.DataFrame(data)
I need to sum up all the times for each week so Week 1 time is 1, Week 2 time is 2+4+6, and Week 3 is 3+5. I also need it to look through the 'Week' column and find all the different weeks, so for this example there are 3 but for another dataframe it could be 2 or 4.
End result is look through a column in a dataframe, find the unique values (1,2,3,...n), groupby be each of those values into rows and sum up the time for each of those values.
I have tried a handful of ways but nothing is really working how I would like. I appreciate any help or ideas.
Expected Output:
Sum
Week 1: 1 1
Week 2: 2 4 6 12
Week 3: 3 5 8
The output can be either individual dataframes of the data or one dataframe that has all three rows with all the numbers and the sum at the end.
Upvotes: 0
Views: 822
Reputation:
You can groupby
"Week", select column "Time", and you can pass multiple functions (such as list
constructor and sum
) to Groupby.agg to do the things you want:
out = dfx.groupby('Week')['Time'].agg(Times=list, Total=sum)
Output:
Times Total
Week
1 [1] 1
2 [2, 4, 6] 12
3 [3, 5] 8
Upvotes: 1
Reputation: 347
import pandas as pd
data = {'Name': ['ch1', 'ch2', 'ch3', 'ch4', 'ch5', 'ch6'],
'Time': [1,2,3,4,5,6],
'Week' : [1, 2, 3, 2, 3, 2]
}
dfx = pd.DataFrame(data)
dfx = dfx.groupby('Week')['Time'].sum()
print(dfx)
output:
Week
1 1
2 12
3 8
Upvotes: 1