Below is my groupby function and dataset before the operation. However, the statement as written produces no change. I want this to be a single row containing sums for each category.
JavaScript
x
2
1
ichiro_df.groupby('playerID')['AB', 'HBP', 'SF'].sum()
2
JavaScript
1
9
1
playerID AB HBP SF
2
81816 suzukic01 692 8.0 4.0
3
83144 suzukic01 647 5.0 5.0
4
84474 suzukic01 679 6.0 1.0
5
85829 suzukic01 704 4.0 3.0
6
87152 suzukic01 679 4.0 6.0
7
88529 suzukic01 695 5.0 2.0
8
89915 suzukic01 678 3.0 2.0
9
Advertisement
Answer
You need to enclose your list by []
:
JavaScript
1
9
1
# HERE ---v-------------------v
2
out = ichiro_df.groupby('playerID')[['AB', 'HBP', 'SF']].sum()
3
print(out)
4
5
# Output
6
AB HBP SF
7
playerID
8
suzukic01 4774 35.0 23.0
9