Skip to content
Advertisement

pandas groupby ignoring column

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.

ichiro_df.groupby('playerID')['AB', 'HBP', 'SF'].sum()
        playerID   AB  HBP   SF
81816  suzukic01  692  8.0  4.0
83144  suzukic01  647  5.0  5.0
84474  suzukic01  679  6.0  1.0
85829  suzukic01  704  4.0  3.0
87152  suzukic01  679  4.0  6.0
88529  suzukic01  695  5.0  2.0
89915  suzukic01  678  3.0  2.0

Advertisement

Answer

You need to enclose your list by []:

#                          HERE ---v-------------------v
out = ichiro_df.groupby('playerID')[['AB', 'HBP', 'SF']].sum()
print(out)

# Output
             AB   HBP    SF
playerID                   
suzukic01  4774  35.0  23.0
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement